How to implement an array filled with objects inside a class in VBA -
i have tried implement several different ways , missing something.
class cunit includes array of cpanel objects -
private ppanel() cpanel public property panel(optional rindex integer) cpanel if rindex = 0 panel = ppanel else panel = ppanel(rindex) end if end property public property let panel(optional rindex integer, value variant) if rindex = 0 ppanel = value else ppanel(rindex) = value end if end property body of module:
function unitbom(unit cunit) cunit set unitbom = unit dim panels() cpanel redim panels(1 unitbom.numpanels) dim paneltemp cpanel set paneltemp = new cpanel dim variant = 1 unitbom.numpanels set panels(i) = paneltemp next unitbom.panel = panels() this works
panels(1).width = 1 when run "object variable or block variable not set."
unitbom.panel(1).width = 2 you guys have forgive me because have not explained well. need array of cpanel objects inside cunit object. problem when create cunit object, dont know how big make cpanel array. attempting create temporary array proper size know size should , set cunit array equal temporary array in order "size" it. there far better way of accomplishing same thing.
from can guess out of (few) pieces of code of yours, there coding errors think derive code logic reviewed:
get property
code:
public property panel(optional rindex integer) cpanel if rindex = 0 panel = ppanel else panel = ppanel(rindex) end if end property should rewritten because since returning panel type object (...as cpanel) should add set keyword before each panel occurrence in left side of assignment statements:
public property panel(optional rindex integer) cpanel if rindex = 0 set panel = ppanel else set panel = ppanel(rindex) end if end property but again in case rindex = 0 error since you'd try set panel type object array of panel objects, quite not same!
so adopt variant approach:
public property panel(optional rindex integer) variant if rindex = 0 panel = ppanel else set panel = ppanel(rindex) end if end property which still (successfully) allow such statement as:
unitbom.panel(1).width = 2 but (unsuccessfully) allow such statement as:
unitbom.panel().width = 2 which (and hopefully, too) don't need, nevertheless class allow ...
so maybe review functionality of cunit class and, instance, adopt default rindex:
public property panel(optional rindex integer) cpanel if rindex = 0 rindex = 1 set panel = ppanel(rindex) end property let property
this used such statement as:
unitbom.panel(1) = unitbom.panel(2) to have both panel(1) , panel(2) refer same panel object
but have use set keyword once again
public property let panel(optional rindex integer, value variant) if rindex = 0 ppanel = value else set ppanel(rindex) = value end if end property
Comments
Post a Comment