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
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.
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 PropertyUsing 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 ClassIn 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
Object Oriented Programming Articles
- Abstract class constructors
- abstract class inheritance in c#.net
- abstract class inheritance in visual basic.net
- New C# Language Feature: Automatic Properties
- Calling base class constructor in C#
- base keyword in c#
- Compile time polymorphism - Method OverLoading
- How to initialize Base class Object from inside Derived Class constructor
- Constructor overloading in vb.net
- How to use inheritance in c#
- How to use inheritance in visual basic.net
- Parameter array in vb.net
- Procedure OverLoading in vb.net
- Visual Inheritance with Windows Forms
- Azure Platform
- GridView CommandField example
- GridView and DetailsView Master/Detail page using SqlDataSource control
- POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
- Query entity data model using linq to entities
- Difference between arraylist and list collection
- How to create a Web service using Visual Studio.net
- FormView DataBound Event
- Calling base class constructor in C#
- Convert a sequence to a generic list using ToList()method
- Project ideas for students
- Accounting Software
- Creating an ASP.Net MVC 3 application
- Using assemblies in .net applications
- How to implement form validation using ASP.Net 2.0 Validation Controls
- Constructors in Visual Basic.Net