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. 4 5687
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.
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
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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,...
|
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
|
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;
};
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |