VB.NET Cheat Sheet
Core VB.NET syntax covering variables, control flow, classes and inheritance, collections, and common data types.
2 PagesBeginnerApr 5, 2026
Basics
Variables and console output.
vbnet
Module Program Sub Main() Dim name As String = "World" Console.WriteLine($"Hello, {name}!") Dim x As Integer = 42 Console.WriteLine("Value: " & x) End SubEnd Module
Control Flow
Conditionals and loops.
vbnet
Dim x As Integer = 10If x > 5 Then Console.WriteLine("big")ElseIf x = 5 Then Console.WriteLine("equal")Else Console.WriteLine("small")End IfFor i As Integer = 1 To 5 Console.WriteLine($"i = {i}")NextDim count As Integer = 0While count < 3 count += 1End While
Classes & Inheritance
Defining a base class and a derived class.
vbnet
Public Class Animal Public Property Name As String Public Sub New(name As String) Me.Name = name End Sub Public Overridable Sub Speak() Console.WriteLine($"{Name} makes a sound") End SubEnd ClassPublic Class Dog Inherits Animal Public Sub New(name As String) MyBase.New(name) End Sub Public Overrides Sub Speak() Console.WriteLine($"{Name} barks") End SubEnd Class
Collections
Generic collections and LINQ.
- Dim list As New List(Of Integer)- Generic strongly-typed list
- list.Add(5)- Appends an item to a collection
- Dim dict As New Dictionary(Of String, Integer)- Key/value map
- For Each item In list ... Next- Iterates a collection
- Array.Sort(arr)- Sorts an array in place
- list.Where(Function(x) x > 5)- LINQ filter (requires Imports System.Linq)
Data Types
Common built-in types.
- Integer- 32-bit signed integer
- Double- Double-precision floating point
- String- Immutable text type
- Boolean- True/False value
- Nothing- Null reference / default value
- Dim x As Integer?- Nullable value type
Error Handling
Structured exception handling with Try/Catch/Finally.
vbnet
Try Dim result As Integer = 10 \ 0Catch ex As DivideByZeroException Console.WriteLine("Cannot divide by zero: " & ex.Message)Catch ex As Exception When ex.Message.Contains("fatal") Console.WriteLine("Filtered: " & ex.Message)Finally Console.WriteLine("Always runs")End TryThrow New ArgumentException("bad input", NameOf(result))
LINQ Queries
Query and transform collections with LINQ.
vbnet
Dim nums = {5, 3, 8, 1, 9, 2}' Query syntaxDim evens = From n In nums Where n Mod 2 = 0 Order By n Select n' Method syntaxDim total = nums.Where(Function(n) n > 3).Sum()Dim names = people.GroupBy(Function(p) p.City) _ .Select(Function(g) g.Key)
Properties & Auto-Properties
Encapsulate fields with property syntax.
vbnet
Public Class Person ' Auto-property with default Public Property Name As String = "Unknown" ' Full property with backing field Private _age As Integer Public Property Age As Integer Get Return _age End Get Set(value As Integer) If value < 0 Then Throw New ArgumentException() _age = value End Set End Property ' Read-only Public ReadOnly Property IsAdult As Boolean Get Return _age >= 18 End Get End PropertyEnd Class
Async & Await
Asynchronous programming with the Task-based model.
vbnet
Public Async Function FetchDataAsync() As Task(Of String) Using client As New HttpClient() Dim response = Await client.GetStringAsync("https://api.example.com") Return response End UsingEnd Function' Await multipleDim results = Await Task.WhenAll(FetchDataAsync(), FetchDataAsync())
String Functions
Common string manipulation functions and methods.
- String.Format- interpolate values into a template: String.Format("{0:C}", price)
- $"..."- interpolated string literal: $"Hello {name}"
- .Substring(i, len)- extract portion of a string
- .Split(","c)- break string into an array by delimiter
- .Trim() / .TrimStart()- remove leading/trailing whitespace
- .Replace(a, b)- swap all occurrences of a substring
- .StartsWith / .Contains- boolean substring tests
- String.IsNullOrWhiteSpace- true when null, empty, or blank
Pro Tip
Enable Option Strict On at the top of your files to catch implicit narrowing conversions at compile time instead of runtime InvalidCastExceptions.
Was this cheat sheet helpful?
Explore Topics
#VBNET#VBNETCheatSheet#Programming#Beginner#ControlFlow#ClassesInheritance#Collections#DataTypes#OOP#DataStructures#CheatSheet#SkillVeris