If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] Duplicating class instance.

Hi, I have a custom class with lots of variables (some simple, others are instances of other classes) and a few graphic objects. I'm writing a wizard to set the class up and was now wondering how to implement the use of the same wizard to modify and existing item of that class. The main problem would be the implementation of a cancel option.

This yields two paths: if I work on the actual instance of the class, I'd have to keep track of changes so that I can go back to the original in case of cancel. The second (and imo easier) option is to create a new one, copy all the contents of the original, work on that one pseudo applying changes in a virtual way and re-copy the new one to the original in case of "apply" or just dispose it in case of "cancel" and refresh with the original untouched data.

I'm not interested in duplicating the graphic objects, but I do have members that should be deep-copied. That is, the class members of the mother class will have to be instantiated or something.

Is there an easy way of cloning custom classes or will I have to write my own method?
And perhaps wishing for an obvious solution I'm not considering, does anyone now a good way to implement "cancelling" changes to something?

Re: Duplicating class instance.

What kind of data needs to be duplicated. For example, arrays have a copy-to method, some other objects use Clone(). You'll have to check the docs, and maybe even test for reference-(in)equality yourself.

What people sometimes do (or... often enough) is that they serialize their class, like, to a temporary file on a disk, and then load the data back in a different instance.

Re: Duplicating class instance.

Well, I have 3 classes: MeassurePoint, Product and Ingredients. My app has a list of Points, each has a Product and each Product has a list of Ingredients.

Each Ingredient has a ChartArea, Series and Legends associated, a List<myStruct> and regular variables (int, string, etc)
Each Product has a List<Ingredients> and regular variables.
Each Point has ToolStripMenuItems, List<TimeSpan> and other variables.

Mostly, it's just the regular variables I'm interested in preserving (IDs, names, settings, series line color, charArea limits and all that), but it's a lot of stuff! If there is any way to do something like a bit-copy of the original class, I could save it's previous status and make any changes I want.

Re: Duplicating class instance.

Yes, that helps a lot! I would have had to write a copy contructor as BioPhysEngr suggested anyway, since some contents should not be copied identically, like ID's or other unique tags, but now I could merge both: clone the objectcs and modify only the unique-assigned values.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.