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?
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.
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.
One way that is nice is to set up a copy constructor.
public class Example
//The normal constructor
//The copy constructor
public Example(Example source)
this.a = source.a;
this.b = source.b;
//Create a new array and copy values into it
this.array = new int[array.Length];
//Call the copy constructor of AnotherClass
this.class = new AnotherClass(source.class);
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
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.