none
Two object are not independent RRS feed

  • Question

  • I want to test the value of an object before and after a routine. I set two objects of the same type to the starting value, and change only one, however at the end of the routine the two object are equal. All changes to one object show up in the other. What am I doing wrong.

      Private DF As New ReportFieldObjects.PersReport
        Public oldDF As New ReportFieldObjects.PersReport
        Public Property DataFields As ReportFieldObjects.PersReport
            Get
                Return DF
            End Get
            Set(value As ReportFieldObjects.PersReport)
                DF = value
                oldDF = value
                oldDF.Name = "54321"
                DF.Name = "12345"
                
    End Property

    Even in this simple example oldDf.name gets a value of "12345" I don't understand this behavior.

    Thursday, March 2, 2017 2:06 AM

Answers

  • Ok I wound up coping the object to a memory stream, and from there to a new object as such...

      DF = value
     Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim stream As New IO.MemoryStream()
            formatter.Serialize(stream, value)
            stream.Seek(0, IO.SeekOrigin.Begin)
            oldDF = CType(formatter.Deserialize(stream), objectType)
    df.name="12345"
    oldDf="54321"
    This gives me a non referenced copy of my original object

    Thursday, March 2, 2017 3:45 AM
  • Thanks... But how do I get two independent objects? 

    You create a clone method for your object.   A clone method creates a new instance, and then populates the properties of that instance using the original as the source, and returns the new instance as the return value of the method.

    Thursday, March 2, 2017 4:55 AM

All replies

  • Welcome to reference types. The variables DF, value and oldDF reference the same object. No matter via which reference you change it, it's the same object.

    Armin

    Thursday, March 2, 2017 2:18 AM
  • Welcome to reference types. The variables DF, value and oldDF reference the same object. No matter via which reference you change it, it's the same object.

    Armin

    Thanks... But how do I get two independent objects? 
    Thursday, March 2, 2017 3:07 AM
  • Ok I wound up coping the object to a memory stream, and from there to a new object as such...

      DF = value
     Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim stream As New IO.MemoryStream()
            formatter.Serialize(stream, value)
            stream.Seek(0, IO.SeekOrigin.Begin)
            oldDF = CType(formatter.Deserialize(stream), objectType)
    df.name="12345"
    oldDf="54321"
    This gives me a non referenced copy of my original object

    Thursday, March 2, 2017 3:45 AM
  • Thanks... But how do I get two independent objects? 

    You create a clone method for your object.   A clone method creates a new instance, and then populates the properties of that instance using the original as the source, and returns the new instance as the return value of the method.

    Thursday, March 2, 2017 4:55 AM
  • Ok I wound up coping the object to a memory stream, and from there to a new object as such.

    You need to be very careful using that technique.  You do not have any control over whether a property of the object that gets copied to the new object is a copy of the reference, or a new instance.  It is likely to be a copy of the reference, and you end up with exactly the problem you started with, except at the level of a property of the object rather than the object itself.

    Thursday, March 2, 2017 4:59 AM
  • Thanks.  This was easier and more clear than my solution.

      Public Function Clone() As object
            Return DirectCast(Me.MemberwiseClone(), object)
        End Function

    Thursday, March 2, 2017 1:20 PM
  • Thanks.  This was easier and more clear than my solution.

      Public Function Clone() As object
            Return DirectCast(Me.MemberwiseClone(), object)
        End Function

    That's a shallow clone:

    https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx


    "One who has no vices also has no virtues..."

    Thursday, March 2, 2017 1:49 PM