Home >>VB.Net Built-in Functions >VB.Net program of constructor overloading
Here, to initialize a data member, we will create a Sample class that contains the default and parameterized constructor.
Program :The source code to demonstrate the overloading of the constructor is given below. The program given is compiled and successfully executed.
'VB.net program to demonstrate the constructor overloading.
Module Module1
Class Sample
Private num As Integer
Public Sub New()
Console.WriteLine("Default or no argument constructor called to initialize data members")
num = 10
End Sub
Public Sub New(ByVal n As Integer)
Console.WriteLine("Parameterized constructor called to initialize data members")
num = n
End Sub
Public Sub Print()
Console.WriteLine("Value of num: {0}", num)
End Sub
End Class
Sub Main()
Dim obj1 As New Sample()
Dim obj2 As New Sample(20)
obj1.Print()
obj2.Print()
End Sub
End Module
We have built a Module1 module in the program above. Here, a sample class containing a data member num has been created. No argument, parameterized constructor, and a Print() method are found in the Sample class.
Initialized data members are used for all constructors, and the Print() method is used to print data member values on the console screen.
The Main() method is the entry point for the program, where we created the Sample Class object and then printed the data member num value on the console screen.