ADVERTISEMENT
ADVERTISEMENT

VB.Net program to demonstrate the constructor overloading

Here, we are going to demonstrate the constructor overloading in VB.Net.
Submitted by Nidhi, on December 25, 2020

Here, we will create a Sample class that contains the default and parameterized constructor to initialize data member.

Program/Source Code:

The source code to demonstrate the constructor overloading is given below. The given program is compiled and executed successfully.

'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

Output:

Default or no-argument constructor called to initialize data members
Parameterized constructor called to initialize data members
Value of num: 10
Value of num: 20
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 no argument, parameterized constructor, and a Print() method.

Both constructors are used to initialized data members and the Print() method is used to print the value of data member 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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.