Constructor overloading in vb.net

When the same method name is used for more than one method, with different types of parameters and returned types, then the method is said to be overloaded. Constructor is a special method called 'New()' in vb.net and is defined as a Sub.

Overloading feature is used most frequently to overload the constructor. We overload the constructor by defining more than one 'Sub New()' procedure. By overloading a constructor, we make available more than one constructor. So, while creating an object we can choose which constructor we want to use to instantiate the object. The only condition while overloading the constructor is that the constructor should have a signature which is different from other constructors.

To demonstrate the use of overloaded constructors we will create a new class to represent Account. We will also create two constructors for an Account class. This gives the flexibility to create objects in different ways as per our requirement.

Note: Sometimes objects are instantiated from UI code and some objects are instantiated by other objects residing in a webserver.

In the following code, one constructor (Constructor2) does not take arguments and another constructor (Constructor1) takes arguments. The Constructor1 is used while creating an instance of the class 'Account' and initializing the class at the same time. The Constructor2 is used to only create the instance of the class. This example is a demonstration of Constructor Overloading.

Declaration of Account Class

Public Class Account
    Private mCode As String   		
    Private mName As String    		
    Private mdescription As String  
    Protected mBalance As Double
    
Constructor1: Constructor to initialize all the member variables.

Public Sub New(ByVal code, ByVal name, 
					ByVal description, ByVal balance)
	mCode = code
	mName = name
	mdescription = description
	mBalance = balance
End Sub

Constructor2:    
Public Sub New()
End Sub
Public Property Code() As String
	Get                                     
		Return mCode
	End Get
	Set(ByVal value As String)              
		mCode = value
	End Set
End Property
Public Property Name() As String
	Get
		Return mName
	End Get
	Set(ByVal value As String)
		mName = value
	End Set
End Property
Public Property description() As String
	Get
		Return mdescription
	End Get
	Set(ByVal value As String)
		mdescription = value
	End Set
End Property
Public Property Balance() As Double
	Get
		Return mBalance
	End Get
	Set(ByVal value As Double)
		mBalance = value
	End Set
End Property
Using Constructor overloading in vb.net The process of instantiating objects by using overloaded constructors is same as how we do it with a single constructor.

To test how Overloaded constructors work, we create a windows form and place the textbox controls on the form. We also place the Ok button to save data which are entered in the textbox controls into the member variables and display the same data. The code for the AccountForm is given below.

The below code creates an instance of the class 'Account' and initializes the class at the same time.

Note : When we compile the code, the signatures of the constructors are compared and the matching constructor is called.

Public Class AccountForm
    Private Sub OkButton_Click(ByVal sender As System.Object, 
    	ByVal e As System.EventArgs) Handles OkButton.Click
        'Instantiate the Account Class Object by using the Constructor1
        Dim myPartyAcc As Account = New 
        	Account(txtCode.Text, txtName.Text, 
            txtdescription.Text, txtBalance.Text)
        MessageBox.Show(myPartyAcc.Code)
        MessageBox.Show(myPartyAcc.Name)
        MessageBox.Show(myPartyAcc.description)
    End Sub
End Class
In the below code, we create the instance of the class and initialize member variables with data entered in the textbox controls by assigning them explicitly.

Public Class AccountForm
    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    								Handles OkButton.Click
        'Instantiate the Account Class Object by using the Constructor2     
        Dim myPartyAcc As Account = New Account()
        myPartyAcc.Code = txtCode.Text
        myPartyAcc.Name = txtName.Text
        myPartyAcc.description = txtdescription.Text
        myPartyAcc.Balance = txtBalance.Text
       
        MessageBox.Show(myPartyAcc.Code)
        MessageBox.Show(myPartyAcc.Name)
        MessageBox.Show(myPartyAcc.description)
   End Sub
End Class