473,237 Members | 1,155 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 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 5661
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
13
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.