Connecting Tech Pros Worldwide Help | Site Map

Copy Custom Objects

  #1  
Old November 13th, 2005, 09:32 PM
Jules
Guest
 
Posts: n/a
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
  #2  
Old November 13th, 2005, 09:32 PM
Tu-Thach
Guest
 
Posts: n/a

re: Copy Custom Objects


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
[color=blue]
>-----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
>.
>[/color]
  #3  
Old November 13th, 2005, 09:32 PM
Christopher Dundon
Guest
 
Posts: n/a

re: Copy Custom Objects


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[color=blue]
>-----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
>[color=green]
>>-----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[/color][/color]
an[color=blue][color=green]
>>object in an other object??
>>
>>
>>Jules
>>.
>>[/color]
>.
>[/color]
  #4  
Old November 15th, 2005, 05:44 AM
Lucean Morningside
Guest
 
Posts: n/a

re: Copy Custom Objects


"Tu-Thach" <tuthach@yahoo.com> wrote in message news:<075d01c34d34$c3439450$a001280a@phx.gbl>...[color=blue]
> 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.[/color]

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Needing some help with Business Objects in ASP.Net PLEASE Guy Thornton answers 6 November 9th, 2006 09:05 PM
Custom Serializable Objects Techno_Dex answers 8 September 16th, 2006 02:15 AM
Objects referencing smae location HankD answers 2 August 17th, 2006 03:45 PM
How to Deep Copy properly? Jax answers 3 November 15th, 2005 05:21 PM