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

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 15164
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
4
by: Arno R | last post by:
Hi all, I have a situation where I (have to) develop a database together with some other person. Let's say we have develop1.mdb and develop2.mdb We need to work on different 'pieces' of the app....
0
by: berg | last post by:
All, I am binding an ArrayList full of custom objects to an DataGrid. The property in the custom class are all public. I define a DataGridTableStyle and set the MappingName to "ArrayList". I...
4
by: Alvo von Cossel I | last post by:
hi, i have been asked to make a good-looking app for a friend. i have an options form with a big tabstrip in it. 1. how can i customize it e.g. change from the standard system style...
7
by: Jed | last post by:
I have a web service method that returns an array of custom objects marked serializeable with fully described public properties. When I bind the results to a DataGrid I can access the properties...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
3
by: =?Utf-8?B?R2hpc3Rvcw==?= | last post by:
Hi all, We have a N-Tier framework and we now create a Web Site App to wotk with this architecture. I add reference from my libraries in the project and creating page and controls is very...
1
by: =?Utf-8?B?VGVycnk=?= | last post by:
I am brand new to using Crystal Reports and am trying to generate a report based on a custom object - in another project. I have a layered design, with all my business objects in a seperate...
19
by: jsanshef | last post by:
Hi, after a couple of days of script debugging, I kind of found that some assumptions I was doing about the memory complexity of my classes are not true. I decided to do a simple script to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.