Copy of class instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Pratt

    #1

    Copy of class instance

    I'm sorry to ask such a fundamental question, but is it possible to create a
    copy of an object in VB.Net 2005?

    I have an app which, on load, creates an instance of a class, into which it
    reads a load of data from XML. What I want is to be able to declare a new
    instance of this class and make it a copy of the existing instance. This
    way I can spawn various instances with their own updated properties

    The code "Dim NewInst as MyClass = OldInst" creates a reference to the
    existing instance and, therefore, changing any properties of NewInst are
    reflected in OldInst.

    Is there a simple way to do what I need?

    Many thanks for any help.


  • Chris

    #2
    Re: Copy of class instance

    Chris Pratt wrote:
    I'm sorry to ask such a fundamental question, but is it possible to create a
    copy of an object in VB.Net 2005?
    >
    I have an app which, on load, creates an instance of a class, into which it
    reads a load of data from XML. What I want is to be able to declare a new
    instance of this class and make it a copy of the existing instance. This
    way I can spawn various instances with their own updated properties
    >
    The code "Dim NewInst as MyClass = OldInst" creates a reference to the
    existing instance and, therefore, changing any properties of NewInst are
    reflected in OldInst.
    >
    Is there a simple way to do what I need?
    >
    Many thanks for any help.
    >
    >
    I don't know of a way to automatically do this, but you really need to
    make a clone method and then inside the clone method set all the
    properties to what they are in the current instance.

    You may be able to do this with reflection in just a couple lines of
    code. This sample code will do all the public properties.

    Dim NewInstance As New IPEGCompany
    Dim myType As Type = GetType(IPEGCom pany)
    Dim myProperties() As Reflection.Prop ertyInfo =
    myType.GetPrope rties((Reflecti on.BindingFlags .Public Or
    Reflection.Bind ingFlags.Instan ce))
    ' Loop thru the public properties
    Dim PropertyItem As Reflection.Prop ertyInfo
    For Each PropertyItem In myProperties
    PropertyItem.Se tValue(NewInsta nce,
    Reader.GetValue (Reader.GetOrdi nal(PropertyIte m.Name)), Nothing)
    Next

    Comment

    • Jim Wooley

      #3
      Re: Copy of class instance

      I'm sorry to ask such a fundamental question, but is it possible to
      create a copy of an object in VB.Net 2005?
      >
      I have an app which, on load, creates an instance of a class, into
      which it reads a load of data from XML. What I want is to be able to
      declare a new instance of this class and make it a copy of the
      existing instance. This way I can spawn various instances with their
      own updated properties
      >
      The code "Dim NewInst as MyClass = OldInst" creates a reference to the
      existing instance and, therefore, changing any properties of NewInst
      are reflected in OldInst.
      >
      Is there a simple way to do what I need?
      >
      You need to have your object implement IClonable and use the .Clone method
      to get a copy of your object. You will be responsible for doing the cloan,
      either with a serialize dehydrate/hydrate mechanism or manual property setting.
      Jim Wooley



      Comment

      • Chris Pratt

        #4
        Re: Copy of class instance

        Thanks Chris, that was really helpful. It allowed me to write a generic
        function which I can use for all cases, as below:
        Public Sub CopyObject(ByVa l ObjectType As Type, _
        ByVal ExistingInstanc e As Object, _
        ByRef NewInstance As Object)

        Dim Props() As Reflection.Prop ertyInfo = _
        ObjectType.GetP roperties(Refle ction.BindingFl ags.Public Or _
        Reflection.Bind ingFlags.Instan ce)

        For Each PropItem As Reflection.Prop ertyInfo In Props

        If PropItem.CanWri te Then
        PropItem.SetVal ue(NewInstance, _
        PropItem.GetVal ue(ExistingInst ance, Nothing), Nothing)
        End If

        Next

        End Sub

        Incidentally, what does the Reader object refer to in your example? Is this
        a better way of extracting the property values than the way I have done it?

        "Chris" <no@spam.comwro te in message
        news:ubFB$1asGH A.4596@TK2MSFTN GP04.phx.gbl...
        Chris Pratt wrote:
        >I'm sorry to ask such a fundamental question, but is it possible to
        >create a copy of an object in VB.Net 2005?
        >>
        >I have an app which, on load, creates an instance of a class, into which
        >it reads a load of data from XML. What I want is to be able to declare a
        >new instance of this class and make it a copy of the existing instance.
        >This way I can spawn various instances with their own updated properties
        >>
        >The code "Dim NewInst as MyClass = OldInst" creates a reference to the
        >existing instance and, therefore, changing any properties of NewInst are
        >reflected in OldInst.
        >>
        >Is there a simple way to do what I need?
        >>
        >Many thanks for any help.
        >
        I don't know of a way to automatically do this, but you really need to
        make a clone method and then inside the clone method set all the
        properties to what they are in the current instance.
        >
        You may be able to do this with reflection in just a couple lines of code.
        This sample code will do all the public properties.
        >
        Dim NewInstance As New IPEGCompany
        Dim myType As Type = GetType(IPEGCom pany)
        Dim myProperties() As Reflection.Prop ertyInfo =
        myType.GetPrope rties((Reflecti on.BindingFlags .Public Or
        Reflection.Bind ingFlags.Instan ce))
        ' Loop thru the public properties
        Dim PropertyItem As Reflection.Prop ertyInfo
        For Each PropertyItem In myProperties
        PropertyItem.Se tValue(NewInsta nce,
        Reader.GetValue (Reader.GetOrdi nal(PropertyIte m.Name)), Nothing)
        Next

        Comment

        Working...