Visual Basic Cheat Sheet
Learn Visual Basic from the basics →
Variables & Types
Dim name As String = "Ada"- Declare a variable with a type
Dim count = 0- Type inferred (Option Infer On)
Const TaxRate As Decimal = 0.2D- Constant
Integer / Long / Double / Decimal / Boolean / Char / String / Date- Everyday types
CInt("42") / CDbl(x) / CStr(n) / CType(obj, T)- Conversion functions
Integer.TryParse(text, n)- Safe parsing (returns True/False)
Option Strict On- File header: forbid implicit narrowing conversions
Strings
"Hello, " & name- Concatenation (use &, not +)
$"Total: {total:F2}"- Interpolated string with a format
s.Length / s.ToUpper() / s.Trim()- Common members
s.Substring(0, 3) / s.Replace("a", "b")- Substring (zero-based) / replace
s.Split(","c) / String.Join(", ", items)- Split / join ("c"c is a Char literal)
"She said ""hi"""- Escape a quote by doubling it
Operators
+ - * / \ Mod ^- Arithmetic; \ is integer division, ^ is power
= <> < > <= >=- Comparison (= is also assignment)
AndAlso / OrElse / Not- Short-circuit logic (And/Or evaluate both sides)
x += 1- Compound assignment
obj Is Nothing / obj IsNot Nothing- Reference checks
If(value, fallback)- Fallback when value is Nothing
Control Flow
If x > 0 Then ... ElseIf x < 0 Then ... Else ... End If- Conditional
Select Case n : Case 1 To 5 : ... : Case Is > 5 : ... : End Select- Multi-way branch with ranges
For i = 1 To 10 Step 2 ... Next- Counting loop (inclusive)
For Each item In items ... Next- Loop over a collection
Do While cond ... Loop / Do ... Loop Until cond- Condition loops
Exit For / Continue For- Leave a loop / skip to the next iteration
Procedures
Sub Greet(name As String) ... End Sub- Procedure with no return value
Function Square(x As Integer) As Integer : Return x * x : End Function- Function with a return value
ByRef total As Integer- Parameter the procedure can change
Optional greeting As String = "Hello"- Optional parameter with a default
ParamArray values() As Integer- Any number of arguments
Function(x) x * 2- Lambda expression
Classes
Public Class Person ... End Class- Class declaration
Public Property Name As String- Auto-implemented property
Public Sub New(name As String)- Constructor
Inherits Animal / Overrides / Overridable / MustOverride- Inheritance keywords
Implements IComparable(Of Person)- Implement an interface
Shared Function Create() As Person- Shared (static) member
Me / MyBase- Current instance / base class
Collections & LINQ
Dim nums() As Integer = {1, 2, 3}- Array literal; nums(0) is the first item
New List(Of String) From {"a", "b"}- Generic list with initializer
New Dictionary(Of String, Integer)- Key/value dictionary
From n In nums Where n > 1 Select n * 10- LINQ query syntax
nums.Where(Function(n) n > 1).Sum()- LINQ method syntax
Errors, Files & Async
Try ... Catch ex As IOException ... Finally ... End Try- Exception handling
Throw New ArgumentException("bad value")- Raise an exception
Using reader As New StreamReader(path) ... End Using- Dispose a resource automatically
File.ReadAllLines(path) / File.WriteAllText(path, text)- Quick file access (System.IO)
Async Function LoadAsync() As Task(Of String)- Asynchronous function
Dim text = Await client.GetStringAsync(url)- Wait for a task without blocking
Tooling
dotnet new console -lang VB -o MyApp- Create a console project
dotnet run / dotnet build / dotnet test- Run, build, and test
dotnet new winforms -lang VB- Windows Forms app (Windows only)
dotnet add package Newtonsoft.Json- Add a NuGet package