0

I am studying VB.NET and I have a question about copying Class instance.

For example, I have

Public Class frmMain_CLT

  Public Shared GEN_SETTING As MY_OBJ

  Public Sub frmMain_CLT_Load(sender As Object, e As EventArgs) Handles MyBase.Load          
    Dim Test_GM As MY_OBJ ' I would like to copy GEN_SETTING to Test_GM
  End Sub

End Class

I would like to copy GEN_SETTING to Test_GM

I know that I can use "Test_GM = GEN_SETTING". However, Test_GM will reference GEN_SETTING. Therefore, Data, where is in the GEN_SETTING will change when I modify Data in the Test_GM.

I would like to make Copy of GEN_SETTING to Test_GM , which is "NOT" referencing original Copy.

But i am not sure how to do it. I was thinking about DirectCast to copy instance but I am getting Error..

Update

I just created Clone method in the MY_OBJ class

for example,

 Public Function ShallowCopy() As MY_OBJ
       Return DirectCast(Me.MemberwiseClone(), MY_OBJ)
    End Function 

However, I am not really sure if it is the best way to copy class instance

Bob Ma
  • 197
  • 5
  • 11
  • 22
  • possible duplicate of [VB.NET copy class instance to instance in another class](http://stackoverflow.com/questions/25025996/vb-net-copy-class-instance-to-instance-in-another-class) – Ňɏssa Pøngjǣrdenlarp Aug 07 '14 at 21:22
  • Hi Plutonix. NO, it is NOT duplicate question. That question was asking about "PASS BY REF" and This question asking about "PASS BY VALUE". Anyway, thanks for help me – Bob Ma Aug 07 '14 at 21:36

1 Answers1

3

This is usually accomplished implementing a function in the original class that creates a Clone of the instance referenced.

For example

Sub Main
    Dim p = new Person() With {.FirstName = "Steve", .LastName = "King"}
    Dim p1 = p.Clone()
    p1.FirstName = "Steven"

    Console.WriteLine(p.FirstName)
    Console.WriteLine(p1.FirstName)
End Sub

Public Class Person
    Public FirstName As String
    Public LastName as String

    Public Function Clone() as Person
        return DirectCast(Me.MemberwiseClone(), Person) 
    End Function

End Class

Here you ask the instance p of the class Person to create a Clone of itself using the base function MemberWiseClone(). Things become more complex if the class to be cloned contains internal reference to other classes.

For example, if our class Person contains an instance of a class Address, then you need to have the Clone method also for the class Address and call the Address.Clone to create a different instance of the Address class in the cloned instance.

Sub Main
    Dim p = new Person() With {.FirstName = "Steve", .LastName = "King"}
    p.AddressInfo = new Address() With {.City = "Rome"}

    Dim p1 = p.Clone()
    p1.FirstName = "Steven"
    p1.AddressInfo.City = "Venice"

    Console.WriteLine(p.FirstName)
    Console.WriteLine(p.AddressInfo.City)
    Console.WriteLine(p1.FirstName)
    Console.WriteLine(p1.AddressInfo.City)
End Sub

Public Class Person
    Public FirstName As String
    Public LastName as String
    Public AddressInfo as Address

    Public Function Clone() as Person
        Dim temp = DirectCast(Me.MemberwiseClone(), Person) 
        temp.AddressInfo = DirectCast(Me.AddressInfo.Clone(), Address)
        return temp        
    End Function

End Class


Public Class Address
    Public City as String
    Public Function Clone() as Address
        return DirectCast(Me.MemberwiseClone(), Address) 
    End Function
End Class

A interesting question about these concepts is What is the difference between a Deep Copy and a Shallow Copy

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • HI Steve. Thanks for helping me. I was thinking about DirectCast method. And, it , now, is working. However, I just wanna know that if DirectCast is the best way or not. thanks :) – Bob Ma Aug 07 '14 at 21:31
  • DirectCast is needed for MemberWiseClone, otherwise you should copy your property one by one in the new instance – Steve Aug 07 '14 at 21:35
  • Hum...Yep, you are right :). I thk, i need to use MemberWiseClone method. Thanks! :) – Bob Ma Aug 07 '14 at 21:41