Visual Basic Copy Constructor

In visual basic, Copy Constructor is a parameterized constructor which contains a parameter of the same class type. The copy constructor in visual basic is useful whenever we want to initialize a new instance to the values of an existing instance.

 

In simple words, we can say copy constructor is a constructor which copies a data of one object into another object. Generally, visual basic won’t provide a copy constructor for objects but we can implement ourselves based on our requirements.

Visual Basic Copy Constructor Syntax

Following is the syntax of defining a copy constructor in visual basic programming language.

 

Class User

    ' Parameterized Constructor

    Public Sub New(ByVal a As String, ByVal b As String)

        ' your code

    End Sub

    ' Copy Constructor

    Public Sub New(ByVal user As User)

        ' your code

    End Sub

End Class

If you observe the above syntax, we created a copy constructor with a parameter of same class type and it helps us to initialize a new instance to the values of an existing instance.

Visual Basic Copy Constructor Example

Following is the example of creating a copy constructor to initialize a new instance to the values of an existing instance in visual basic programming language.

 

Module Module1

    Class User

        Public name, location As String

        ' Parameterized Constructor

        Public Sub New(ByVal a As String, ByVal b As String)

            name = a

            location = b

        End Sub

        ' Copy Constructor

        Public Sub New(ByVal user As User)

            name = user.name

            location = user.location

        End Sub

    End Class

    Sub Main()

        ' User object with Parameterized constructor

        Dim user As User = New User("Suresh Dasari", "Hyderabad")

        ' Another User object (user1) by copying user details

        Dim user1 As User = New User(user)

        user1.name = "Rohini Alavala"

        user1.location = "Guntur"

        Console.WriteLine(user.name & ", " & user.location)

        Console.WriteLine(user1.name & ", " & user1.location)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created an instance of copy constructor (user1) and using an instance of the user object as a parameter type. So, the properties of user object will be sent to user1 object and we are changing the property values of user1 object but those will not effect the user object property values.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Copy Constructor Example Result

 

If you observe the above result, we initialized a new instance to the values of an existing instance but those changes not effected the existing instance values.

 

This is how we can use copy constructor in visual basic programming language to create a constructor with the parameter of same class type based on our requirements.