Constructors in Visual Basic

A constructor is a method that automatically runs when an instance is created. In VB, the constructor is always a method with the name New().

Example

Class Example1
    Public Sub New()
        Console.WriteLine("statements here called automatically")
    End Sub
End Class

Module Module1
    Sub Main()
        Dim obj As New Example1()

        Console.ReadLine()
    End Sub

End Module

Output

statements here called automatically
Share

You may also like...