Constructor in Visual Basic .NET

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Visual Basic .NET

A constructor is a special method that is automatically called when an instance of an object is created. In Visual Basic .NET, constructors are used to initialize the object's properties and perform any necessary setup operations.

The syntax for a constructor in Visual Basic .NET is similar to that of any other method, but it has no return type and is named after the class. Here is an example:

Public Class Person
    Public FirstName As String
    Public LastName As String

Public Sub New(ByVal fName As String, ByVal lName As String)
FirstName = fName
LastName = lName
End Sub
End Class

In this example, we have a Person class with two properties, FirstName and LastName. The class also has a constructor that takes two parameters (fName and lName) and sets the corresponding properties.

To create an instance of the Person class using this constructor, we would use the following code:

Dim person As New Person("John", "Smith")

This creates a new Person object with the FirstName property set to "John" and the LastName property set to "Smith".

It's important to note that a class can have multiple constructors with different parameter lists. For example:

Public Class Person
    Public FirstName As String
    Public LastName As String

Public Sub New()
FirstName = "Unknown"
LastName = "Unknown"
End Sub

Public Sub New(ByVal fName As String)
FirstName = fName
LastName = "Unknown"
End Sub

Public Sub New(ByVal fName As String, ByVal lName As String)
FirstName = fName
LastName = lName
End Sub
End Class

In this modified example, we have three different constructors with varying parameter lists. The first constructor sets the FirstName and LastName properties to default values, the second constructor only takes a FirstName parameter, and the third constructor takes both FirstName and LastName parameters.

We can create a Person object using any of these constructors, depending on our needs. For example:

Dim person1 As New Person() ' Uses the default constructor
Dim person2 As New Person("John") ' Uses the constructor that takes a first name parameter
Dim person3 As New Person("John", "Smith") ' Uses the constructor that takes first and last name parameters

In conclusion, constructors are an essential part of creating and initializing objects in Visual Basic .NET. A well-designed constructor can simplify the process of creating and using objects in your code.