👉 Read the full lesson on DevLadder (free, lesson 4 of 12 in the VB.NET: Visual Basic .NET from Basics to Advanced (with C# Comparison) course)
' Sub = method returning nothing (C# void)
Public Sub Log(message As String)
Console.WriteLine($"{Date.Now:HH:mm} {message}")
End Sub
' Function = method returning a value
Public Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
' ByVal (default) copies the value/reference; ByRef passes the variable itself (C# ref)
Public Sub Swap(ByRef a As Integer, ByRef b As Integer)
Dim t = a : a = b : b = t
End Sub
' Optional parameters and named arguments
Public Sub SendMail(toAddr As String, Optional subject As String = "Hi", Optional urgent As Boolean = False)
End Sub
SendMail("a@b.com", urgent:=True)
' ParamArray = C# params
Public Function Sum(ParamArray values As Integer()) As Integer
Return values.Sum()
End Function
' Lambdas
Dim square = Function(x As Integer) x * x
Dim show = Sub(msg As String) Console.WriteLine(msg)
Dim isAdult As Func(Of Integer, Boolean) = Function(age) age >= 18
Overloading uses the same name with different parameters (Overloads keyword is optional except when overloading inherited members).
An interview question on VB.NET
ByVal vs ByRef?
ByVal (the default) passes a copy of the value: for reference types, a copy of the reference, so the method can modify the object but not replace the caller's variable. ByRef passes the variable itself, so assigning a new value inside the method changes the caller's variable (C# ref).
Can you answer these?
- And vs AndAlso, Or vs OrElse?
- How are events handled in VB.NET?
- DirectCast vs CType vs TryCast?
- You must maintain a large VB.NET Web Forms app and gradually move it to modern .NET. What is your plan?
The answers, plus 7 more VB.NET interview questions and a mock-interview mode, are free on DevLadder.
Next in the course: Arrays, generics and collections →
Top comments (0)