Results 1 to 4 of 4

Thread: Copying complex class objects

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    8

    Question Copying complex class objects

    I would like to be able to create a exact copy of a class object, in order to implement a rollback mechanism for that class. The idea is that the object can be rolled back to a previous version. At key points in time the object must be able to make a copy of itself and keep the copy in memory until needed.

    The problem is that the class is complex, with a deep containment hierarchy. To complicate matters, some of these contained objects are instances of classes defined in other components.

    I don't want to take the approach of creating a new object of the class and (recursively) copying each of the properties across, as this would be quite inefficient and actually impractical.

    Is there any way to achieve this? Perhaps it is possible to use a memory block copy.

    Cheers

    Jonathan

  2. #2
    jim mcnamara
    Guest
    Create a property bag, write the object to disk using the bag. To rollback, read the object from disk.

    IF you want to have an exact copy in memory, write then immediately read it back into a new object.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    8
    Jim

    Thanks for your help with this.

    There is only one snag with this method.

    If I am right, it appears that to write a property to a PropertyBag object the class of the object being written must be persistable (by the VB definition of persistable).

    Currently, all of the objects in the containment hierarchy are not persistable. This is not a problem in itself, but to make them persistable would require them to be publically creatable. Making some of these classes publically creatable would be dangerous, as they only have meaning within the context of a containment hierarchy of their owner.

    Indeed most of the classes within this project simply implement interfaces defined in an external type library, and as a result are only privately creatable. They can of course be exposed publically once created, although they have no interfaces of their own.

    Is there any other way to do this other than with the PropertyBag class?

    Thanks for your help, it is one of the most sensible answers put forward so far!

    Cheers



    Jonathan

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Unfortunately, there is no easy way around this issue. I have done some extensive research, and have not found any worthy work arounds. The way that I got around this problem was by creating a generic Clone routine, that simply copied over the data to a temporary object. My object hiearch was very complex, but this was the only way that I found.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width