Memberwise assignment of an object.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Frey

    Memberwise assignment of an object.

    Can someone please tell me if there is any way to do this easily in C#:

    x = new MyClass( );
    y = new MyClass( );

    I want to copy the values from y to x, without it resulting in the
    construction of a new instance of MyObject( ). It is a deep copy, but
    without the new instance that comes from using ICloneable.Clon e( ) (MyClass
    itself comprises just ints and strings).

    The reason I am looking for this behaviour is I want to have "binder"
    objects associated with the members of "x". If the x object reference
    changes, obviously the binders won't be able to see the underlying changes
    being made (since they will still be looking at the old reference).

    This may not even be the best way to go about this. This is trivial to do in
    C++ so I'm a little lost in terms of doing the same thing in C#. Of course,
    my strategy might be entirely inappropriate for C# so I'm happy for others
    to suggest a better strategy.


  • Ranjan

    #2
    Re: Memberwise assignment of an object.

    Hello Kevin,

    I think you would have to copy all the properties, one after the other manually.
    ..Net BCL doesn;t provide deep copies as such. You can implement you own DeepCopy
    method though, which deep copies an object onto another.

    HTH,
    r.

    [color=blue]
    > Can someone please tell me if there is any way to do this easily in
    > C#:
    >
    > x = new MyClass( );
    > y = new MyClass( );
    > I want to copy the values from y to x, without it resulting in the
    > construction of a new instance of MyObject( ). It is a deep copy, but
    > without the new instance that comes from using ICloneable.Clon e( )
    > (MyClass itself comprises just ints and strings).
    >
    > The reason I am looking for this behaviour is I want to have "binder"
    > objects associated with the members of "x". If the x object reference
    > changes, obviously the binders won't be able to see the underlying
    > changes being made (since they will still be looking at the old
    > reference).
    >
    > This may not even be the best way to go about this. This is trivial to
    > do in C++ so I'm a little lost in terms of doing the same thing in C#.
    > Of course, my strategy might be entirely inappropriate for C# so I'm
    > happy for others to suggest a better strategy.
    >[/color]



    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Memberwise assignment of an object.

      Kevin Frey <kevin_g_frey@h otmail.com> wrote:[color=blue]
      > Can someone please tell me if there is any way to do this easily in C#:
      >
      > x = new MyClass( );
      > y = new MyClass( );
      >
      > I want to copy the values from y to x, without it resulting in the
      > construction of a new instance of MyObject( ). It is a deep copy, but
      > without the new instance that comes from using ICloneable.Clon e( ) (MyClass
      > itself comprises just ints and strings).[/color]

      That doesn't sound like a deep copy at all - it just sounds like a
      shallow copy, which is what you get with the simplest implementation of
      ICloneable.Clon e() (one which calls Object.Memberwi seClone).
      [color=blue]
      > The reason I am looking for this behaviour is I want to have "binder"
      > objects associated with the members of "x". If the x object reference
      > changes, obviously the binders won't be able to see the underlying changes
      > being made (since they will still be looking at the old reference).[/color]

      Hmm... that's somewhat different. x isn't an object, it's a variable. I
      suspect what you really want is a wrapper which holds a reference to a
      MyClass, and allows you to set which reference it's looking at at any
      one time.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Bruce Wood

        #4
        Re: Memberwise assignment of an object.

        I tend to implement Clone() for my objects like this:

        public object Clone()
        {
        Abcd other = new Abcd();
        this.CopyTo(oth er);
        }

        protected void CopyTo(Abcd other)
        {
        ...
        }

        The CopyTo then copies the fields of this object to the "other" object.

        Of course, there is no law against making CopyTo public, which is, I
        think, what you're after.

        Comment

        • Nick Hounsome

          #5
          Re: Memberwise assignment of an object.


          "Kevin Frey" <kevin_g_frey@h otmail.com> wrote in message
          news:eO67yGXHGH A.3000@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Can someone please tell me if there is any way to do this easily in C#:
          >
          > x = new MyClass( );
          > y = new MyClass( );
          >
          > I want to copy the values from y to x, without it resulting in the
          > construction of a new instance of MyObject( ). It is a deep copy, but
          > without the new instance that comes from using ICloneable.Clon e( )
          > (MyClass itself comprises just ints and strings).
          >
          > The reason I am looking for this behaviour is I want to have "binder"
          > objects associated with the members of "x". If the x object reference
          > changes, obviously the binders won't be able to see the underlying changes
          > being made (since they will still be looking at the old reference).[/color]

          Simply copying fields wont trigger binders - you need to raise the change
          events - either through copying properties or using a method.
          [color=blue]
          >
          > This may not even be the best way to go about this. This is trivial to do
          > in C++ so I'm a little lost in terms of doing the same thing in C#. Of
          > course,[/color]

          It isn't trivial in C++ - you have to write an assignment operator for any
          non-trivial object.

          Even if you relied on default shallow copy you'd still have the problem of
          triggering the binders.
          [color=blue]
          > my strategy might be entirely inappropriate for C# so I'm happy for others
          > to suggest a better strategy.[/color]

          The DataSet class is designed with awareness of this problem - you would
          typically use Clear and Merge or just Merge rather than just assigning a new
          DataSet

          Having said that it is quite easy to write generic rebinding methods to be
          triggered by assignment to a property of type MyClass.


          Comment

          Working...