vba - Function uses intellisense to accept multiple Enums -
i have enum declared in vba
enum myenum vala = 3 valb = 17 valc = 20 end enum i function accepts combination of values specified enum, , employs intellisense when enter them. i.e., prompts me possible values can input.
now if choose values in enum powers of two, chip pearson suggests in his section on composite values, namely
function myfunction(val myenum) '... end function usage
debug.print myfunction(vala + valb) 'with intellisense however enums not follow pattern, there's no way can think of discern whether vala + valb input, or valc example. therefore i'm wondering:
is there way pass array/undetermined number of variables function, whilst still using intellisense suggest possible values?
no, there isn't. flag enums use powers of 2 that.
public enum foo v1 = 2 ^ 0 v2 = 2 ^ 1 v3 = 2 ^ 2 v4 = 2 ^ 3 v5 = 2 ^ 4 '... end enum the reason allow use bitwise logic determine bits "on", given composite value, example 5 can 4 + 1:
power 2^5 2^4 2^3 2^2 2^1 2^0 value 32 16 8 4 2 1 bit 0 0 0 1 0 1 as already mentioned, way use paramarray parameter, can ever variant, , can ever passed byref, not means lose intellisense, lose semantic safety of passing parameter values byval (well, code implicitly passes byref anyway).
so either come way "map" individual "magic underlying values" enum members flag enum members (with powers of 2), or forfeit intellisense.
private function getflag(byval value myenum) myflagenum static converter scripting.dictionary if converter nothing set converter = new scripting.dictionary converter .add myenum.vala, myflagenum.valuea .add myenum.valb, myflagenum.valueb .add myenum.valc, myflagenum.valuec .add myenum.vald, myflagenum.valued .add myenum.vale, myflagenum.valuee end end if getflag = converter(value) end function and can combine them after pulling mapped value:
debug.print myfunction(getflag(vala) + getflag(valb)) ' intellisense everywhere! where myfunction takes myflagenum value.
if underlying values have specific meaning , they're carved in stone , myfunction needs use them, need function "unwrap" individual flagenum values individual "myenum" values:
private function getfromflag(byval value myflagenum) myenum static converter scripting.dictionary if converter nothing set converter = new scripting.dictionary converter .add myflagenum.valuea, myflagenum.vala .add myflagenum.valueb, myflagenum.valb .add myflagenum.valuec, myflagenum.valc .add myflagenum.valued, myflagenum.vald .add myflagenum.valuee, myflagenum.vale end end if getfromflag = converter(value) end function and again, use function with individual not combined values. first thing myfunction needs determine individual flags "on", , getfromflag each 1 of them individual non-flag enum values.
this should useful:
private function hasflag(byval composite long, byval flag long) boolean hasflag = (composite , flag) = flag end function
Comments
Post a Comment