excel - Is it possible to change the type of a variable or function in VBA during runtime? -
i riddling if possible conditionally switch type of function or variable between enum types.
something this:
public enum enmtest ea = 1 eb = 2 ec = 3 end enum public enum enmdemo ea = 10 eb = 50 ec = 100 end enum public function demofunction() enmdemo dim edemo enmdemo redim edemo enmtest redim demofunction enmdemo end function 'this not work, there no way make work? public sub test() debug.print demofunction().ea 'should 1 end sub 'this not work, there no way make work? public sub test2 dim temp variant temp = demofunction() debug.print temp.eb 'should 2 end sub basically goal have variable dim myvar might enuma or enumb type. these enums might identicall, except values.
my guess won't work @ no angle, because of way vba handles enums. make sure explanation, have gut feeling after hour of experimenting.
my current workaround, demonstrates goal:
public enum enmtest ea = 1 eb = 2 ec = 3 end enum public enum enmdemo ea = 10 eb = 50 ec = 100 end enum public function demo() debug.print str(getvalues(1)(1)) 'prints 1 debug.print str(getvalues(2)(1)) 'prints 10 end function public function getarray(val1, val2, val3) variant dim result variant redim result(1 3) result(1) = val1 result(2) = val2 result(3) = val3 getarray = result end function public function getvalues(myint integer) variant if (myint = 1) getvalues = getarray(enmdemo.ea, enmdemo.eb, enmdemo.ec) else getvalues = getarray(enmtest.ea, enmtest.eb, enmtest.ec) end if end function
the best can offer custom conversion function each enum type. although echo dans comment: consider why want this.
' write 1 of these each conversion want function casttodemo(byref v enmtest) enmdemo select case v case enmtest.ea casttodemo = enmdemo.ea case enmtest.eb casttodemo = enmdemo.eb case enmtest.ec casttodemo = enmdemo.ec end select end function ' use public sub test() dim enmtest dim b enmdemo = enmtest.ea b = casttodemo(a) debug.print b end sub
Comments
Post a Comment