Copy of an object - Problem with array

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I have the following class:

VB.NET:
Public Class Class1

    Private someDates(1) As Date

    Public Property SomeDate1() As Date
        Get
            Return someDates(0)
        End Get
        Set(ByVal value As Date)
            someDates(0) = value
        End Set
    End Property

    Public Property SomeDate2() As Date
        Get
            Return someDates(1)
        End Get
        Set(ByVal value As Date)
            someDates(1) = value
        End Set
    End Property

    Public Shared Function Copy(ByVal c As Class1) As Class1
        Return DirectCast(c.MemberwiseClone, Class1)
    End Function

End Class

When I do the following:

Create a variable of Class1, sets both dates, creates another variable using the Copy function (copying the first variable), then sets the dates to something else
The problem (as you would probably have guessed) is that when I set the dates for the second variable, the dates for the first variable will also be set

The problem seems to be with arrays/list of only. Does anybody here know how to fix the problem?
 
I'm still learning about custom classes so if my description below is not correct, I'll be happy if someone corrects it :D

When you copy your class, you are copying references to the arrays rather than copying the array itself. What you need to do is to "deep copy" the object, so it creates complete copies of the arrays themselves.
 
MemberwiseClone only does a shallow copy it says, you may try the CloneObject method here. You have to mark your class as serializable, the class members to be serialized must also be serializable for it to work.
 
That I know, the problem is that whenever I search for how to do a deep copy, they give me the above mentioned line (or something very unfriendly that takes 100+ lines of code)

DirectCast(c.MemberwiseClone, Class1)

It works fine with normal properties, but not for arrays and lists :(
 
As an idea, this should work :-

VB.NET:
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c1 As Class1 = New Class1

        c1.SomeDate0 = New Date(2008, 4, 21)
        c1.SomeDate1 = New Date(2008, 4, 22)

        Dim c2 As Class1 = DirectCast(CloneObject(c1), Class1)
        c2.SomeDate0 = New Date(2008, 5, 1)
        c2.SomeDate1 = New Date(2008, 5, 2)

        MessageBox.Show(c1.SomeDate1.ToString)
        MessageBox.Show(c2.SomeDate1.ToString)



    End Sub

    Private Function CloneObject(ByVal obj As Object) As Object
        Dim ms As New MemoryStream(1000)
        Dim bf As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))
        bf.Serialize(ms, obj)
        ms.Seek(0, SeekOrigin.Begin)
        CloneObject = bf.Deserialize(ms)
        ms.Close()
    End Function

End Class

Edit: you all beat me to it :)
 
Back
Top