excel vba - Passing class object by 'property get' in a parent class does not affect the nested class object -
i have hierarchy of classes defined follows
cquestion:
private ptext string private sub class_initialize() ptext = "" end sub property let text(t string) ptext = t end property property text() string text = ptext end property
cquestionlist:
private pqlist() new cquestion private plistlen integer private sub class_initialize() plistlen = 0 end sub public sub addend(q string) plistlen = plistlen + 1 redim preserve pqlist(1 plistlen) pqlist(plistlen).text = q end sub public function format() string dim integer if plistlen = 0 formatlist = "there no questions in category" + vbnewline else formatlist = "questions:" + vbnewline = 1 plistlen formatlist = formatlist + "• " + pqlist(i).text + vbnewline next end if end function
ccategory:
private pname string private pqlist new cquestionlist private sub class_initialize() pname = "" end sub property questionlist() cquestionlist set questionlist = pqlist end property property let name(n string) pname = n end property property name() string name = pname end property
when attempt call category.questionlist.addend "question here"
, not throw errors. when subsequently call msgbox category.questionlist.format
blank message box. not sure how ends blank, seeing format should returning text. doing wrong here? have looked @ other examples of passing class objects let , within parent class, , cannot see how doing different. suggestions?
example code:
dim c new ccategory c.questionlist.addend "a question" c.questionlist.addend "another question" msgbox c.questionlist.format
put option explicit
@ top of each module , you'll see problem immediately:
public function format() string dim integer if plistlen = 0 formatlist = "there no questions in category" + vbnewline '^^^^ variable not defined. else formatlist = "questions:" + vbnewline = 1 plistlen formatlist = formatlist + "• " + pqlist(i).text + vbnewline next end if end function
you either need change public function format() string
public function formatlist() string
or change formatlist
assignments format
.
i'd go formatlist
naming avoid collisions format
function.
Comments
Post a Comment