Getting Started
3 snippetsVB.NET basics
Hello World
Module Program
Sub Main()
Console.WriteLine("Hello, World!")
End Sub
End ModuleComments
' Single line comment
REM Alternative comment
' TODO: Multi-line comments use continuation
' with apostrophes on each lineImports
Imports System
Imports System.Collections.Generic
Imports System.LinqVariables & Data Types
5 snippetsType declarations
Variable Declaration
Dim name As String = "John"
Dim age As Integer = 30
Dim price As Decimal = 19.99D
Dim isActive As Boolean = TrueType Inference
Dim x = 10 ' Integer
Dim s = "Hello" ' String
Dim d = 3.14 ' DoubleConstants
Const PI As Double = 3.14159
Const MAX_SIZE As Integer = 100Common Types
Integer, Long, Short, Byte
Double, Single, Decimal
Boolean, Char, String
Date, DateTime, ObjectNullable Types
Dim age As Integer? = Nothing
Dim name As String = Nothing
If age.HasValue Then
Console.WriteLine(age.Value)
End IfOperators
4 snippetsArithmetic and logical operations
Arithmetic
Dim sum = a + b
Dim diff = a - b
Dim prod = a * b
Dim quot = a / b ' Real division
Dim intQuot = a \ b ' Integer division
Dim remainder = a Mod b
Dim power = a ^ bComparison
a = b ' Equal
a <> b ' Not equal
a > b ' Greater
a < b ' Less
a >= b ' Greater or equal
a <= b ' Less or equalLogical
If a > 0 And b > 0 Then
If a > 0 Or b > 0 Then
If Not flag Then
If a > 0 AndAlso b > 0 Then ' Short-circuit
If a > 0 OrElse b > 0 Then ' Short-circuitString Operations
Dim full = first & " " & last ' Concatenation
Dim full2 = first + " " + last
Dim repeat = "x" * 5 ' Not available, use New String("x"c, 5)Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Control Flow
4 snippetsConditional statements
If Statement
If age >= 18 Then
Console.WriteLine("Adult")
Else
Console.WriteLine("Minor")
End IfElseIf
If score >= 90 Then
grade = "A"
ElseIf score >= 80 Then
grade = "B"
Else
grade = "F"
End IfSelect Case
Select Case dayOfWeek
Case 1
Console.WriteLine("Monday")
Case 2 To 5
Console.WriteLine("Weekday")
Case 6, 7
Console.WriteLine("Weekend")
Case Else
Console.WriteLine("Invalid")
End SelectInline If
Dim result = If(condition, valueIfTrue, valueIfFalse)
Dim msg = If(age >= 18, "Adult", "Minor")Loops
7 snippetsIteration constructs
For Loop
For i As Integer = 1 To 10
Console.WriteLine(i)
NextFor Step
For i As Integer = 10 To 1 Step -1
Console.WriteLine(i)
NextFor Each
Dim fruits() As String = {"Apple", "Banana", "Orange"}
For Each fruit In fruits
Console.WriteLine(fruit)
NextWhile Loop
While count < 10
Console.WriteLine(count)
count += 1
End WhileDo While
Do While count < 10
Console.WriteLine(count)
count += 1
LoopDo Until
Do Until count >= 10
Console.WriteLine(count)
count += 1
LoopExit and Continue
For i = 1 To 100
If i = 50 Then Exit For
If i Mod 2 = 0 Then Continue For
Console.WriteLine(i)
NextFunctions & Subroutines
5 snippetsProcedures with and without returns
Sub (No Return)
Sub Greet(name As String)
Console.WriteLine("Hello, " & name)
End Sub
' Call
Greet("Alice")Function (With Return)
Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
' Call
Dim sum = Add(5, 3)Optional Parameters
Function Power(base As Integer, Optional exp As Integer = 2) As Integer
Return CInt(Math.Pow(base, exp))
End Function
Dim x = Power(5) ' 25
Dim y = Power(2, 3) ' 8ByRef Parameters
Sub Swap(ByRef a As Integer, ByRef b As Integer)
Dim temp = a
a = b
b = temp
End SubLambda Expressions
Dim square = Function(x) x * x
Dim result = square(5) ' 25Arrays & Collections
6 snippetsData structures
Array Declaration
Dim numbers(4) As Integer ' 5 elements (0-4)
Dim names() As String = {"Alice", "Bob", "Charlie"}Array Access
numbers(0) = 10
Console.WriteLine(numbers(0))Array Functions
Dim length = numbers.Length
Array.Sort(numbers)
Array.Reverse(numbers)List
Dim list As New List(Of Integer)
list.Add(10)
list.Add(20)
list.Remove(10)
Console.WriteLine(list.Count)Dictionary
Dim dict As New Dictionary(Of String, Integer)
dict.Add("Alice", 25)
dict("Bob") = 30
Console.WriteLine(dict("Alice"))LINQ
Dim numbers() = {1, 2, 3, 4, 5}
Dim evens = numbers.Where(Function(n) n Mod 2 = 0).ToArray()
Dim doubled = numbers.Select(Function(n) n * 2).ToList()Object-Oriented Programming
4 snippetsClasses and objects
Class Definition
Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Sub New(name As String, age As Integer)
Me.Name = name
Me.Age = age
End Sub
End ClassCreate Instance
Dim person As New Person("John", 30)
Console.WriteLine(person.Name)Inheritance
Public Class Employee
Inherits Person
Public Property Salary As Decimal
End ClassInterface
Interface IDrawable
Sub Draw()
End Interface
Class Circle
Implements IDrawable
Public Sub Draw() Implements IDrawable.Draw
Console.WriteLine("Drawing circle")
End Sub
End Class