473,385 Members | 2,015 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,385 software developers and data experts.

Cloning

How to clone objects which contain other objects and collections.

1. In top level class (Class1), write a clone method as follows:

public object Clone()
{
Class1 object=(Class1)this.MemberwiseClone();

//clone objects within this class
object.Object2=(Class2)this.Object2.Clone();

//Also clone any collection objects
object.Collection1=(CollectionClass)this.Collectio n1.Clone();

return object;
}

2. For the objects within this class (Class1).
a. Inherit the class from Icloneable by adding ": ICloneable" after
class defn.
b. Add clone method
public object Clone()
{
return this.MemberwiseClone();
}

3. For collections within this class (Class1).
a. Inherit the collection from Icloneable by adding ": ICloneable"
after class defn.
b. Add clone method
public object Clone()
{
return new Collection1(this);
}
c. Write a constructor for collection that takes itself as a parameter
public Collection1(ICollection collection)
{
foreach (CollectionClassObject o in collection)
{
this.Add((CollectionClassObject)o.Clone());
}
}
d. Add a clone method to the CollectionClassObject
public object Clone()
{
return this.MemberwiseClone();
}

4. Ensure that all the objects within the top-level class (Class1)
have all their inner objects and collections cloned too. i.e.
Class1.Object1.Object1 is cloned etc.
Jul 21 '05 #1
1 1685
You can use serialization to perform a deep object clone. Here's an example
(in VB.NET - shouldn't be too hard to convert to C#):

Function CloneObject(ByVal obj As Object) As Object
Dim ms As New System.IO.MemoryStream
Dim bf As New Formatters.Binary.BinaryFormatter(Nothing, New
StreamingContext(StreamingContextStates.Clone))
bf.Serialize(ms, obj)
ms.Seek(0, IO.SeekOrigin.Begin)
Dim oClone As Object = bf.Deserialize(ms)
ms.Close()
Return oClone
End Function

hope that helps..
Imran.

"Stuart" <st**************@gmail.com> wrote in message
news:3c**************************@posting.google.c om...
How to clone objects which contain other objects and collections.

1. In top level class (Class1), write a clone method as follows:

public object Clone()
{
Class1 object=(Class1)this.MemberwiseClone();

//clone objects within this class
object.Object2=(Class2)this.Object2.Clone();

//Also clone any collection objects
object.Collection1=(CollectionClass)this.Collectio n1.Clone();

return object;
}

2. For the objects within this class (Class1).
a. Inherit the class from Icloneable by adding ": ICloneable" after
class defn.
b. Add clone method
public object Clone()
{
return this.MemberwiseClone();
}

3. For collections within this class (Class1).
a. Inherit the collection from Icloneable by adding ": ICloneable"
after class defn.
b. Add clone method
public object Clone()
{
return new Collection1(this);
}
c. Write a constructor for collection that takes itself as a parameter
public Collection1(ICollection collection)
{
foreach (CollectionClassObject o in collection)
{
this.Add((CollectionClassObject)o.Clone());
}
}
d. Add a clone method to the CollectionClassObject
public object Clone()
{
return this.MemberwiseClone();
}

4. Ensure that all the objects within the top-level class (Class1)
have all their inner objects and collections cloned too. i.e.
Class1.Object1.Object1 is cloned etc.

Jul 21 '05 #2

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

Similar topics

4
by: Tom | last post by:
Hey ho, The release of PHP5 seemed like a good reason to try to learn it once more... I'm reading through the O'Reilly PHP/MySQL book and right now, I'm fiddling with objects. I just...
7
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the...
8
by: Tom | last post by:
I've a problem. I want to clone an object having a list of other objects (and so on :/). Do you know any other way than ICloneable.Clone() implementation for all classes in the way? Help..
2
by: Hendrik Schober | last post by:
Hi, I need something like this: class X { private: struct Impl_ { virtual ~Impl_() {} virtual Impl_* clone() const = 0; };
3
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when...
6
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete...
10
by: pazabo | last post by:
Hi, I'm trying to somehow implement polymorphic object cloning (just as it is in Java), but when I write: class Object { public: virtual Object * clone() const = 0; // ... }
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...
1
by: Dan Dorey | last post by:
I've implemented the ICloneable interface on one of my class. I've written this simple code in two different ways and I think both should work but it's not the case and I'm curious to understand...
0
by: Chris | last post by:
Hi All. I'm cloning a treenode, like so: TreeNode trNewTemp = new TreeNode(); trNewTemp = ((((ICloneable)trTempNode).Clone()) as TreeNode); Now the cloning works- trNewTemp has the same...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.