Syntax Basics and Compiler Options

AddHandler AddressOf Alias And AndAlso As Boolean ByRef Byte ByVal Call Case Catch CBool CByte CChar CDate CDbl CDec Char CInt Class CLng CObj Const Continue CSByte CShort CSng CStr CType CUInt CULng CUShort Date Decimal Declare Default Delegate Dim DirectC…

3.1 Statements and Lines

One statement per line, with no semicolons.

Implicit line continuation (VB 10+) after commas, operators, opening parentheses, etc. Explicit continuation uses _ (space underscore).

Multiple statements on one line with : (discouraged): Dim a = 1 : Dim b = 2.

Dim total As Integer = price *
                       quantity
Dim message = "This is a long string " &
              "split across lines"
Dim old = 1 + _
          2              ' explicit continuation (legacy style)

3.2 Case Insensitivity

Dim Name, name, and NAME are the same identifier. Visual Studio auto-corrects casing to match the declaration.

Keywords are case-insensitive too (VS formats them as If, Then, End If).

3.3 Comments

There are no block comments. Select lines and press Ctrl+K, Ctrl+C to comment and Ctrl+K, Ctrl+U to uncomment.

' Single-line comment
REM Old-style comment
''' <summary>XML documentation comment</summary>
''' <param name="x">The input</param>
''' <returns>The result</returns>
Function Square(x As Integer) As Integer
    Return x * x   ' trailing comment
End Function

3.4 Compiler Options (Very Important)

Option Explicit Variables must be declared with Dim Option Strict

Forbids implicit narrowing conversions and late binding Option Infer Allows Dim x = 5 type inference Option Compare

String comparison: case-sensitive (Binary) or not (Text) Always use Option Strict On in new code. It catches type errors at compile time and makes the code faster.

Option Strict On
Option Explicit On
Option Infer On

3.5 Keywords (Selection)

AddHandler AddressOf Alias And AndAlso As Boolean ByRef Byte ByVal Call Case Catch CBool CByte CChar CDate CDbl CDec Char CInt Class CLng CObj Const Continue CSByte CShort CSng CStr CType CUInt CULng CUShort Date Decimal Declare Default Delegate Dim DirectCast Do Double Each Else ElseIf End Enum Erase Error Event Exit False Finally For Friend Function Get GetType Global GoTo Handles If Implements Imports In Inherits Integer Interface Is IsNot Let Lib Like Long Loop Me Mod Module MustInherit MustOverride MyBase MyClass NameOf Namespace Narrowing New Next Not Nothing NotInheritable NotOverridable Object Of On Operator Option Optional Or OrElse Overloads Overridable Overrides ParamArray Partial Private Property Protected Public RaiseEvent ReadOnly ReDim RemoveHandler Resume Return SByte Select Set Shadows Shared Short Single Static Step Stop String Structure Sub SyncLock Then Throw To True Try TryCast TypeOf UInteger ULong UShort Using Variant Wend When While Widening With WithEvents WriteOnly Xor