[RESOLVED] Duplicating class instance.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Duplicating class instance.

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    109

    [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!

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    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.

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    109

    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!

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    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.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  6. #6
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    109

    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!

  7. #7
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center