Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the parameterized constructor

VB.Net program to demonstrate the parameterized constructor

Write a VB.Net program to demonstrate the parameterized constructor

Here, a sample class with a parameterized constructor will be created; the class data members will be initialized by the parameterized constructor.

Program :

Below is the source code to demonstrate the parameterized constructor. The program given is compiled and successfully executed.

'VB.net program to demonstrate the parameterized constructor.


Module Module1
    Class Sample
        Private num As Integer
        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 obj As New Sample(20)
        obj.Print()
    End Sub
End Module
	
Output:
Parameterized constructor called to initialize data members
Value of num: 20
Explanation:

We created a Module1 module in the program above. Here, a class sample containing a data member num has been created. A parameterized constructor and the Print() method are included in the Sample class.

Using the New Keyword, the parameterized constructor is introduced, where we initialized the data member num by the specified parameter. The Print() member is used to print on the console screen the value of the data member num.

The Main() method is the entry point for the program, where we created a Sample Class object and then printed the data member num value on the screen of the console.


No Sidebar ads