Learn VBNET with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
1
VB.NET Counter and Theme Toggle
Module Program
Dim count As Integer = 0
Dim isDark As Boolean = False
Sub UpdateUI()
Console.WriteLine($"Counter: {count}")
Console.WriteLine($"Theme: {(If(isDark, "Dark", "Light"))}")
End Sub
Sub Increment()
count += 1
UpdateUI()
End Sub
Sub Decrement()
count -= 1
UpdateUI()
End Sub
Sub ResetCounter()
count = 0
UpdateUI()
End Sub
Sub ToggleTheme()
isDark = Not isDark
UpdateUI()
End Sub
Sub Main()
' Simulate actions
UpdateUI()
Increment()
Increment()
ToggleTheme()
Decrement()
ResetCounter()
End Sub
End Module
Demonstrates a simple counter with theme toggling using VB.NET variables and console output.
2
VB.NET Simple Calculator
Module Program
Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
Function Subtract(a As Integer, b As Integer) As Integer
Return a - b
End Function
Function Multiply(a As Integer, b As Integer) As Integer
Return a * b
End Function
Function Divide(a As Integer, b As Integer) As Integer
Return a b
End Function
Sub Main()
Console.WriteLine($"Add 5 + 3: {Add(5,3)}")
Console.WriteLine($"Subtract 5 - 3: {Subtract(5,3)}")
Console.WriteLine($"Multiply 5 * 3: {Multiply(5,3)}")
Console.WriteLine($"Divide 6 / 2: {Divide(6,2)}")
End Sub
End Module
Basic arithmetic operations using VB.NET functions.
3
VB.NET Factorial
Module Program
Function Factorial(n As Integer) As Integer
If n <= 1 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function
Sub Main()
Console.WriteLine($"Factorial of 5: {Factorial(5)}")
End Sub
End Module
Recursive factorial function.
4
VB.NET Fibonacci Sequence
Module Program
Function Fibonacci(n As Integer) As Integer
If n = 0 Then Return 0
If n = 1 Then Return 1
Return Fibonacci(n-1) + Fibonacci(n-2)
End Function
Sub Main()
For i As Integer = 0 To 9
Console.WriteLine(Fibonacci(i))
Next
End Sub
End Module
Recursive Fibonacci sequence generator.
5
VB.NET Array Comprehension
Module Program
Sub Main()
Dim numbers = Enumerable.Range(1,10).ToArray()
Dim squares = numbers.Where(Function(n) n Mod 2 = 0).Select(Function(n) n * n).ToArray()
Console.WriteLine(String.Join(",", squares))
End Sub
End Module
Square even numbers using array operations.
6
VB.NET Dictionary Filtering
Module Program
Sub Main()
Dim scores As New Dictionary(Of String, Integer) From {
{"Alice",10}, {"Bob",5}, {"Charlie",12}
}
Dim highScores = scores.Where(Function(kv) kv.Value >= 10).ToDictionary(Function(kv) kv.Key, Function(kv) kv.Value)
For Each kv In highScores
Console.WriteLine($"{kv.Key}: {kv.Value}")
Next
End Sub
End Module
Filter dictionary based on values.
7
VB.NET Anonymous Functions
Module Program
Sub Main()
Dim add = Function(x As Integer, y As Integer) x + y
Console.WriteLine(add(3,7))
End Sub
End Module
Using lambda expressions.
8
VB.NET Sum of Array
Module Program
Sub Main()
Dim numbers = {1,2,3,4,5}
Dim sum = numbers.Sum()
Console.WriteLine(sum)
End Sub
End Module
Sum elements of an array using LINQ.
9
VB.NET Zip Arrays
Module Program
Sub Main()
Dim arr1 = {1,2,3}
Dim arr2 = {4,5,6}
Dim sums = arr1.Zip(arr2, Function(a,b) a+b).ToArray()
Console.WriteLine(String.Join(",", sums))
End Sub
End Module
Combine two arrays element-wise using LINQ.
10
VB.NET Tuple Destructuring
Module Program
Sub Main()
Dim pair = Tuple.Create(3,7)
Dim (x,y) = pair
Console.WriteLine(x + y)
End Sub
End Module
Destructuring a tuple and summing the elements.