Home >>VB.Net Built-in Functions >Copy constructor in VB net with Example

Copy constructor in VB net with Example

Write a Copy constructor in VB net with Example

Here, to initialize the data member, we will create a sample class containing the default and copy constructor.

Program :

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

'VB.Net program to demonstrate the copy constructor.


Module Module1
    Class Sample
        Private num As Integer
        Public Sub New()
            Console.WriteLine("Default or no argument constructor called")
        End Sub
        Public Sub New(ByVal n As Integer)
            Me.New()
            Console.WriteLine("Parameterized constructor called")
            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:
Id: 101
Name: Rohit
Id: 101
Name: Rohit
Explanation:

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

The copy builder is used to initialize an object from other objects, and the Print() method is used to print the console screen value of the data members.

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