vba - How do I to create a copy of a custom object or collection? -
i have custom collection - let's call colparenta
- , contains number of collections called colchild
. want create function creates new collection, colparentb
has of properties , contains same children colparenta
. user can modify few properties of colparentb
need to, rather having redefine ones same colparenta
.
colparentb
should contain new instances of colchild
copies of found in `colparenta.
i can't right?
set colparentb = colparenta colparentb.name = "copy of " & colparenta.name
because makes colparentb
point colparenta
, changes properties of colparenta
right?
i'm confused. in advance.
you correct in suspicions - assigning pointers, it'll reference same instance of object different name.
you're going need create clone functions on colparent , colchild classes. way colchild.clone can memberwise clone , return brand new object same properties, , colparent can create new collection cloned colchild objects.
be careful though, if of properies of colparent or colchild references object may need cloned to avoid updating values don't mean to.
possible functions (note i'm supposing colchild contains number of instances of class clscontent - that'll need changing):
colparent.clone:
public function clone() colparent 'start new parent collection dim parent colparent set parent = new colparent 'populate new collection clones of originals contents dim child colchild each child in me parent.add(child.clone) next set clone = parent end function
colchild.clone:
public function clone() colchild 'start new parent collection dim child colchild set child = new colchild 'populate new collection clones of originals contents dim content clscontent each content in me child.add(content.clone) next set clone = child end function
clscontent.clone:
public function clone() clscontent 'start new parent collection dim content clscontent set child = new clscontent child.property1 = me.property1 child.property2 = me.property2 ... set clone = content end function
please excuse bugs or typos - don't have dev env handy, i'm writing straight text box!
Comments
Post a Comment