469,117 Members | 1,538 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

Copy Custom Objects

I have a cutom object ex.:
public class Client
{
public string Name;
public string Id;
public Adresses Adresses;

public Client()
{
}
}

Client cl = new Client();
cl.Name = "xx";
cl.Adresses.add(adres);
....

Now I want a second client that is a copy of this first
client (not a reference to this first client, it must be
a stand alone copy, with no references to the first).

How I have to do that. Do I have to implement a Copy
method on the class Client? And also a copy on de Class
adresses (who is a collection).
Or are there other ways to simple copy the contents of an
object in an other object??
Jules
Nov 13 '05 #1
3 14981
There is no simple way to do this other than writing the
code yourself. Typically, you would implement
ICloneable.Clone() to clone your Client object.

Tu-Thach
-----Original Message-----
I have a cutom object ex.:
public class Client
{
public string Name;
public string Id;
public Adresses Adresses;

public Client()
{
}
}

Client cl = new Client();
cl.Name = "xx";
cl.Adresses.add(adres);
....

Now I want a second client that is a copy of this first
client (not a reference to this first client, it must be
a stand alone copy, with no references to the first).

How I have to do that. Do I have to implement a Copy
method on the class Client? And also a copy on de Class
adresses (who is a collection).
Or are there other ways to simple copy the contents of an
object in an other object??
Jules
.

Nov 13 '05 #2
That is pretty much what I did to get around this issue.
Implement the ICloneable interface, and within the Clone
method, create a NEW instance of the class and set all the
variables to match the original. If you have objects in
the class, such as ArrayList's, iterate through the list
and copy the data, do not copy the object as a whole or
it'll be a reference as well.

A little testing should let you know if this worked
correctly or not.

Any questions, don't hesitate to ask.

Regards,
Chris
-----Original Message-----
There is no simple way to do this other than writing the
code yourself. Typically, you would implement
ICloneable.Clone() to clone your Client object.

Tu-Thach
-----Original Message-----
I have a cutom object ex.:
public class Client
{
public string Name;
public string Id;
public Adresses Adresses;

public Client()
{
}
}

Client cl = new Client();
cl.Name = "xx";
cl.Adresses.add(adres);
....

Now I want a second client that is a copy of this first
client (not a reference to this first client, it must be
a stand alone copy, with no references to the first).

How I have to do that. Do I have to implement a Copy
method on the class Client? And also a copy on de Class
adresses (who is a collection).
Or are there other ways to simple copy the contents of anobject in an other object??
Jules
.

.

Nov 13 '05 #3
"Tu-Thach" <tu*****@yahoo.com> wrote in message news:<07****************************@phx.gbl>...
There is no simple way to do this other than writing the
code yourself. Typically, you would implement
ICloneable.Clone() to clone your Client object.


The only possible problem with this approach is the added cost of maintaining
the Clone method if you had to modify the class, which isn't much of a problem
once you have the method implemented.

Another possible approach--one that I have used previously--is to mark the
class in question as serializable, and inside the Clone method seralize the
source object into a MemoryStream object, then deserialize it into the copied
object. Works like a charm, if you don't mind the possible performance
overhead, which shouldn't be significant.

-LM
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Arno R | last post: by
4 posts views Thread by Alvo von Cossel I | last post: by
7 posts views Thread by Jed | last post: by
3 posts views Thread by =?Utf-8?B?R2hpc3Rvcw==?= | last post: by
1 post views Thread by =?Utf-8?B?VGVycnk=?= | last post: by
19 posts views Thread by jsanshef | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.