Enums- How to tell If Value not defined?

I would like to handle this generally if possible…

Say you are getting a defined set of values from some other software and you have an enum with all those values… But new versions of that software could potentially add new ones…

Besides maintaining code that gets a list of values in a constant, preference file or database is there a way to find out if an integer value was defined in an enum? If trying to convert an integer to an undefined enum threw an exception one could do:

Dim theEnum As KnownCodesEnum
Try
      theEnum =  KnowCodeEnum(theCode)
Catch EnumException
      theEnum = KnowCodeEnum.Unkown
End if

Select Case theEnum
Case KnowCodeEnum.Unkown
   DoSomething
Case KnowCodeEnum.SomeAction1
   DoAction1
Case KnowCodeEnum.SomeAction2
   DoAction2
...
End Select

But as it is legal to put any integer into any enum that does not work…

Ideally there would be a generic method for enums as fro dictionaries such as :
EnumerationName.Lookup(theInteger As String, NotFoundValue As EnumerationName) As EnumerationName

Then one could just do

Select Case KnowCodeEnum.LookUp(theCode, KnowCodeEnum.Unkown) Case KnowCodeEnum.Unkown DoSomething Case KnowCodeEnum.SomeAction1 DoAction1 Case KnowCodeEnum.SomeAction2 DoAction2 ... End Select

Is there a way to do something like this?

  • Karen

not really
enums are, under the hood, integers so you can shove any value in
theyre an odd data type in Xojo

but if you only code for “known” values then the else is “unknown value”

[sarcasm]
Gee… it they impose VAR because “everyone else is doing it”, why not invoke Enums “like everyone else does”?
[/sarcasm]

You forgot to Var sarcasm first :stuck_out_tongue:

[quote=457438:@Norman Palardy]not really
enums are, under the hood, integers so you can shove any value in
theyre an odd data type in Xojo

but if you only code for “known” values then the else is “unknown value”[/quote]

Actually what I gave is bad example… but easier to explain that what I really want to do.

What I wanted too do was extend your HTTP error code module… to return unknown error if a platform specific error code that is not included in either the XPlatform List you created or the platform specific list. There really is no action to do besides returning the error code.

Having method like Enum.Lookup would make determining that a lot less work.

  • Karen

Except for an Enum defining a new type name they are just integers
I would expect lookup, string name etc would all need to be defined in the compiler
While you can extend an Enum you’d have to do that for EVERY enum type and the compiler could do that a lot more simply but …
File a feature request if you havent