๐Ÿ“˜

Visual Basic Cheat Sheet

Microsoft's readable, English-like language โ€” its classic form powered a generation of Windows desktop apps and Excel macros, and its modern descendant VB.NET is still maintained in huge enterprise codebases today. Copy-paste examples plus live Run/Submit exercises.

Jump to: Your first programVariablesif / else LoopsArraysSubs & functions ๐Ÿš€ Practice problems

Your first program

Module Program
    Sub Main()
        Console.WriteLine("Hello, world!")
    End Sub
End Module
๐Ÿ“ VB uses whole words instead of braces โ€” End Sub, End If, End While โ€” which is what makes it read almost like plain English.

Variables

Dim age As Integer = 25
Dim name As String = "Sam"
Dim price As Double = 19.99
Const Pi As Double = 3.14159

if / else

Dim age As Integer = 20
If age >= 18 Then
    Console.WriteLine("Adult")
ElseIf age >= 13 Then
    Console.WriteLine("Teen")
Else
    Console.WriteLine("Child")
End If

Loops

For i As Integer = 0 To 4
    Console.WriteLine(i)
Next

Dim n As Integer = 3
While n > 0
    Console.WriteLine(n)
    n -= 1
End While

Arrays

Dim nums() As Integer = {10, 20, 30}
Console.WriteLine(nums(0))          ' 10
Console.WriteLine(nums.Length)      ' 3

Subs & functions

VB separates the two โ€” Sub does something, Function returns a value.

Function Add(a As Integer, b As Integer) As Integer
    Return a + b
End Function

๐Ÿš€ Practice problems

Full runnable programs, compiled and run in a real Visual Basic (Mono) sandbox โ€” same workspace as the other cheat sheets.

Sum an array EASY

Module Program
    Sub Main()
        Dim nums() As Integer = {4, 8, 15, 16, 23, 42}
        Dim total As Integer = 0
        For Each n In nums
            total += n
        Next
        Console.WriteLine(total)
    End Sub
End Module

Reverse a string MEDIUM

Module Program
    Function ReverseStr(s As String) As String
        Dim arr() As Char = s.ToCharArray()
        Array.Reverse(arr)
        Return New String(arr)
    End Function

    Sub Main()
        Console.WriteLine(ReverseStr("basic"))
    End Sub
End Module

FizzBuzz MEDIUM

Module Program
    Sub Main()
        For i As Integer = 1 To 15
            If i Mod 15 = 0 Then
                Console.WriteLine("FizzBuzz")
            ElseIf i Mod 3 = 0 Then
                Console.WriteLine("Fizz")
            ElseIf i Mod 5 = 0 Then
                Console.WriteLine("Buzz")
            Else
                Console.WriteLine(i)
            End If
        Next
    End Sub
End Module

Word frequency with a Dictionary HARD

Imports System.Collections.Generic

Module Program
    Sub Main()
        Dim text As String = "the cat sat on the mat the cat ran"
        Dim counts As New Dictionary(Of String, Integer)
        For Each word In text.Split(" "c)
            If Not counts.ContainsKey(word) Then
                counts(word) = 0
            End If
            counts(word) += 1
        Next
        For Each kv In counts
            Console.WriteLine(kv.Key & ": " & kv.Value)
        Next
    End Sub
End Module