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

Here, we are going to demonstrate the default or no-argument constructor in VB.Net.
Submitted by Nidhi, on December 25, 2020 [Last updated : March 06, 2023]

Default or no-argument constructor in VB.Net

Here, we will create a Sample class with the default constructor, the default constructor will initialize the data members of the class.

Program/Source Code:

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

VB.Net code to demonstrate the example of default or no-argument constructor

'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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1. Here, we created a class Sample that contains a data member num. The Sample class contains a default or no-argument constructor and a Print() method.

The default or no-argument constructor is implemented using the New keyword, here we initialized the data member num by 10. The Print() member is used to print the value of data member num on the console screen.

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

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.