Enums and casting

I think I am misunderstanding something… (2019R1.1)

An integer should be able to be casted to an enum even if the enum definition does not have a value for that integer defined… I KNOW that was the intended behavior because I argued with a Xojo (well RS) that one should not be able to do that when enums were first introduced…

But I seem to remember at one time I could just do a simple cast but I now seem to need to use CType. Is my memory wrong?

Dim U8 as Uint8 = Uint8(theEnum) ’ Gives type mismatch error
Dim U8 as Uint8 = Uint8(theEnum) ’ Works IF the integer Enum value is one of the values that was defined in teh enum
BUT:
Dim U8 as Uint8 = Ctype(theEnum, Uint8) ’ Cause a Framework exception in ObjectGlue.h(190) at Runtime for a value not defined in the enum
The framework exception I reported: <https://xojo.com/issue/58596>

Is not being able to do a simple cast a bug or is my memory failing!

BTW as I was raising an exception anyway for an undefined value, this just gave me a different exception than I was expecting… getting there was bug on my part… My code should not have put an undefined value into the enum and that is why I was throwing my own exception!!!

  • karen

Try:

Integer( enum.Item )

[quote=467137:@Kem Tekinay]Try:

Integer( enum.Item )[/quote]

That works as expected and did in my app …

But if I try to replicate the error with an undefined value I was seeing in a simple app I don’t get the framework error… need to dig deeper!!

Just found the issue… it was not what i thought… The framework exception was due to a nil object exception that was suppressed because of a pragma on the same line.

Sorry for the noise

-Karen