471,319 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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.Clone( ) (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.
Jan 20 '06 #1
4 5414
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.

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.Clone( )
(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.


Jan 20 '06 #2
Kevin Frey <ke**********@hotmail.com> wrote:
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.Clone( ) (MyClass
itself comprises just ints and strings).
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.Clone() (one which calls Object.MemberwiseClone).
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).


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 20 '06 #3
I tend to implement Clone() for my objects like this:

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

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.

Jan 20 '06 #4

"Kevin Frey" <ke**********@hotmail.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
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.Clone( )
(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).
Simply copying fields wont trigger binders - you need to raise the change
events - either through copying properties or using a method.

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,
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.
my strategy might be entirely inappropriate for C# so I'm happy for others
to suggest a better strategy.


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.
Jan 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Paul Rubin | last post: by
5 posts views Thread by Haoyu Zhang | last post: by
16 posts views Thread by Edward Diener | last post: by
166 posts views Thread by Graham | last post: by
6 posts views Thread by Neil Zanella | last post: by
13 posts views Thread by Daniel W | last post: by
37 posts views Thread by Tim N. van der Leeuw | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.