vba - Need help adding an custom object to a custom collection -
lets have custom collection , custom object have parent-child relationship
i have userform user names collection , provides input other properties of collection. when click "add parent" click event handled , calls following function:
public function addparent() dim newparent clsparent set newparent = new clsparent 'add parent properties' frmaddparent newparent.name = .cboparentname.value newparent.width = .txtparentwidth.value newparent.xslope = .txtparentcrossslope.value end 'show form creating child object' frmaddlanematerial.show end function the user sees new form creating child object. when user clicks "add child" event handled , calls following function:
public function addchild() dim newchild clschild set newchild = new clschild 'add child properties' frmaddchild newchild.name = .cboparentname.value newchild.layernumber = .cbolayernum.value newchild.xslope = newparent.xslope end 'add new child parent collection' newparent.add newchild end function and user needs able return userform , add child.
the lines won't work are:
newchild.xslope = newparent.xslope and:
newparent.add newchild i 'object required' error.
how i/where add child parent collection?
first of all, newparent local addparent function addchild has no visibility of it. fix that, need move dim newparent clsparent out of addparent function , make module-level variable. assumes addparent , addchild in same module.
secondly, newparent.add newchild work if have written add method in clsparent. if have not you'll need write one. have no idea how plan on using it, following code pretty generic , should pointed in right direction. code go in clsparent module:
private m_ochildren collection sub add(child clschild) if m_ochildren nothing set m_ochildren = new collection m_ochildren.add child end sub this build private collection of clschild objects manipulate using additional methods or expose via property get.
update: address comment, if want keep multiple parents you'll need add collection that. example:
dim parents collection public function addparent() dim newparent clsparent set newparent = new clsparent 'add parent properties' frmaddparent newparent.name = .cboparentname.value newparent.width = .txtparentwidth.value newparent.xslope = .txtparentcrossslope.value end 'initialize collection of parents if has not been done' if parents nothing set parents = new collection 'add new parent object parents collection' parents.add newparent 'show form creating child object' frmaddlanematerial.show end function public function addchild() dim newchild clschild set newchild = new clschild 'add child properties' frmaddchild newchild.name = .cboparentname.value newchild.layernumber = .cbolayernum.value newchild.xslope = newparent.xslope end 'add new child added parent in parent collection' parents.item(parents.count).add newchild end function of course, have collection keep track of. gets tricky keeping track of multiple instances of forms (which assume doing) , easy find unmanageable mess.
Comments
Post a Comment