473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15181
There is no simple way to do this other than writing the
code yourself. Typically, you would implement
ICloneable.Clon e() 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.ad d(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.Clo ne() 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.a dd(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.Clon e() 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
2423
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 writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other. The listboxes are populated with my custom objects (SwapItem), which simply
4
5904
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. For testing purposes I need to 'merge' these two together to another database "Final.mdb" Happily there will be no problem with object-names. (I use a naming convention, other developer does not) From develop2.mdb I have code to -- copy...
0
1695
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 then add a DataGridTextBoxColumn for each of the properties in my custom class which define a MappingName --> property name in class as well as header text. When I run the form, I see correct number of rows displayed, but none of the columns...
4
2326
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 tabcontrol to what i want? 2. if #1 isnt possible to do, what would be the best way to make something look like a tabstrip?
7
2563
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 in the ItemDataBound event of the codebehind but I can't access them declaratively in the HTML code? Here is the code to call the method. net.mysite.www.WSInterface proxy; proxy = new net.mysite.www.WSInterface();
12
5342
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 the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
3
1935
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 easy, using my objects. But, it's seem that I cannot create report using Crystal Report with my objects. I created classes in the app_Code folder and yes, I can use them but, it's not the way I eant to do it. I want to use my classes from my dlls.
1
2200
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 project. I created a new project as part of the solution, added a reference to the project containing my business objects, created a datasource that points to one of the objects and then tried to create CR based on the object. The data tab (in the CR...
19
4647
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 isolate the problem: class MyClass: def __init__(self,s): self.mystring = s
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.