473,386 Members | 1,817 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,386 software developers and data experts.

How to Deep Copy properly?

Jax
I have inherited the ICloneable interface and have set up
a public object Clone() method;
This method creates a new version of itself puts all it's
stuff in it(and yes all custom objects within have the
same method) then casts that into and object and passes it
back.

Is this not a deep copy?
It isn't working, I change the value and it satays changes
on the originals, how do I sort this out?

Many thanks to any help given.

jax
Nov 15 '05 #1
3 6327
"Jax" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
I have inherited the ICloneable interface and have set up
a public object Clone() method;
This method creates a new version of itself puts all it's
stuff in it(and yes all custom objects within have the
same method) then casts that into and object and passes it
back.

Is this not a deep copy?
It isn't working, I change the value and it satays changes
on the originals, how do I sort this out?


You copied the references.
See below:
class CopyExample
{
string str;
public object ShallowCopy()
{
CopyExample ce = new CopyExample();
ce.str = this.str;
return (object)CopyExample;
}
public object DeepCopy()
{
CopyExample ce = new CopyExample();
ce.str = this.str.Clone();
retunr (object)CopyExample;
}
}
Nov 15 '05 #2
Hello

For the String type in particular, Clone method returns a reference to the
string itself, this is because strings in .NET are immutable

Best regards
Sherif

"Ayende Rahien" <Ay****@no.spam> wrote in message
news:#F**************@TK2MSFTNGP09.phx.gbl...
"Jax" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
I have inherited the ICloneable interface and have set up
a public object Clone() method;
This method creates a new version of itself puts all it's
stuff in it(and yes all custom objects within have the
same method) then casts that into and object and passes it
back.

Is this not a deep copy?
It isn't working, I change the value and it satays changes
on the originals, how do I sort this out?


You copied the references.
See below:
class CopyExample
{
string str;
public object ShallowCopy()
{
CopyExample ce = new CopyExample();
ce.str = this.str;
return (object)CopyExample;
}
public object DeepCopy()
{
CopyExample ce = new CopyExample();
ce.str = this.str.Clone();
retunr (object)CopyExample;
}
}

Nov 15 '05 #3
Hello

An easy way to clone an object, is to serialize it to a memory stream, then
unserialize it again.
But this can be done only when the class has [Serializable] attribute
defined, and all fields in the class whether custom or not must have the
serializable attribute.

[Serializable]
public class Class1
{
}

[Serializable]
public class MyClass : ICloneable
{
// all these fields are serializable
int a;
string b;
double c;
decimal[] d;
Class1 e;

// This field is not serializable so we define the NonSerialized
Attribute
[NonSerialized]
TextBox t;

public object Clone()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, bf);
ms.Position = 0;
return bf.Deserialize(ms);
}
}

Best regards,
Sherif

"Jax" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
I have inherited the ICloneable interface and have set up
a public object Clone() method;
This method creates a new version of itself puts all it's
stuff in it(and yes all custom objects within have the
same method) then casts that into and object and passes it
back.

Is this not a deep copy?
It isn't working, I change the value and it satays changes
on the originals, how do I sort this out?

Many thanks to any help given.

jax

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
4
by: Dennis | last post by:
I have several Data Structures, say "mystruct" which contain arrays of bytes, other structures, etc. I then dimension a variable (var1) as "mystruct" and set the various elements var1 to data. I...
6
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new...
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
4
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
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 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.