-
October 28th, 2011, 03:58 PM
#1
[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?
Thanks!
-
October 28th, 2011, 04:48 PM
#2
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.
-
October 31st, 2011, 09:12 AM
#3
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.
Thanks for the help!
-
November 2nd, 2011, 12:43 AM
#4
Re: Duplicating class instance.
One way that is nice is to set up a copy constructor.
For example
Code:
public class Example
{
int a;
float b;
int[] array;
AnotherClass class;
//The normal constructor
public Example()
{
//Whatever
}
//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];
source.CopyTo(this.array,0);
//Call the copy constructor of AnotherClass
this.class = new AnotherClass(source.class);
}
}
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
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.
-
November 8th, 2011, 02:58 AM
#5
Re: Duplicating class instance.
Maybe you are interested in Object.MemberwiseClone().
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
November 8th, 2011, 08:53 AM
#6
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.
Thank you for the help!
-
November 11th, 2011, 01:39 AM
#7
Re: [RESOLVED] Duplicating class instance.
Glad it worked out. Let us know if you have more problems. Good luck!
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|