Home >>VB.Net Built-in Functions >VB.Net program to demonstrate default or no-argument constructor

VB.Net program to demonstrate default or no-argument constructor

Write a VB.Net program to demonstrate default or no-argument constructor

Here, with the default constructor, we can build a sample class, with the default constructor initialize the class data members.

Program :

The source code is provided below to demonstrate the default or no-argument constructor. The program given is compiled and successfully executed.

'VB.net program to demonstrate the

'default or no-argument constructor.


Module Module1
    Class Sample
        Private num As Integer
        Public Sub New()
            Console.WriteLine("Default constructor called to initialize data members")
            num = 10
        End Sub
        Public Sub Print()
            Console.WriteLine("Value of num: {0}", num)
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample()
        obj.Print()
    End Sub
End Module
	
Output:
Default constructor called to initialize data members
Value of num: 10
Explanation:

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

Using the New Keyword, the default or no-argument constructor is introduced, where we initialized the data member num by 10. 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