VB.NET Cheat Sheet
─────────────────────────────────────────
I have been working with VB.NET a lot recently and as I haven’t used it in many years ran into a whole heap of differences between it and C#. So to help with this I have created a cheat sheet of how to do different things between c# and VB.NET. I will keep adding stuff to this as I find them.
Comments
c#:
12345
// line comment
/*
block comment
*/VB.NET:
12
' line comment
rem line commentHex Literals:
C#:
1
var x = 0xAB;VB.NET:
1
Dim x = &HABVAR:
C#:
12
//Variable names in C# are case sensetive
var foo = new List<int>();VB.NET:
123
'Variable names in vb.net are not case sensitive
Dim foo = new List(of int);
Dim foo as new List(of int);Event Listeners:
C#:
12345678
//Declare event
public event EventHandler<bool> MyEvent;
//Raising event
MyEvent(this, false);
//Adding handler to event
obj.MyEvent += myHandlerFunction;VB.NET
1234567891011
'Declare event
Event AnEvent(ByVal EventNumber as Integer)
'Raising events
RaiseEvent AnEvent(Number)
'Declaring object with events
Dim WithEvents MyObject As New EventClass
'Declare method to handle events
Sub MyEventHandler() Handles MyObject.EventNameLambdas:
c#:
1
var foo = (x) => x * 2;VB.NET:
123456
Dim foo = Function(x) x * 2
Multiline:
Dim foo = Function(x)
Return x * 2
End FunctionExtension Methods:
c#:
1234567
Public static class MyExtensionMethods
{
Public static void MyExtensionMethod(this string target, int index)
{
//Do stuff here
}
}VB.NET:
123456
Module MyExtensionMethods
Public sub MyExensionMethod(string target, int index)
'Do stuff here
End Sub
End ModuleProperites:
C#:
12345678910111213141516171819
//Auto property
Public string Foo {get; set;}
//Property
Public string Foo
{
Get
{
return "bar"
}
Set
{
m_foo = value
}
}
//Read only property
Public string Foo {get;}VB.NET:
12345678910111213141516171819
'Auto Property
Public property foo as string
'Property
Public property foo as string
Get
return "foo"
End Get
Set(value as string)
m_foo = value
End Set
End Property
'Read only property
Public readonly property Foo
Get
return "foo"
End Get
End Property═══════════════════════════════════