Convert an string or integer to an enum type

Public Enum JobState InActive=0 Active=1 Success=2

dim x as string = "1" dim s as JobState s = val(x) // This fails to compile.
How do I cast an integer or string to the JobState type?

I had to try this to figure it out.

dim x as string = "1"
dim s as JobState
s = JobState( val(x) )  // This fails to compile.

Sorry, that doesn’t work either. Try:

dim x as string = "1"
dim s as JobState
s = JobState( CType( val(x), Integer ) )  

You can also store val(x) in an integer variable first and give that to JobState.

Thanks Kem!
I wish there was a way to also extend an enumerated type such that one could print out it’s name.
System.debugLog(s.name) // Would print Inactive, Active, Success …

enums take UInt32

Dim i as Uint32 = cdbl( "1" ) Dim s as JobState = jobState( i )

Should do it.

I just found a bug. If you define the Enum with Active="2", and assign it to a variable somewhere, the IDE crashes when you compile.

<https://xojo.com/issue/31855>

I wonder what would happen if the enum was from 0 to 3 and you assigned “4”.

[quote=60538:@Brian O’Brien]Public Enum JobState InActive=0 Active=1 Success=2

dim x as string = "1" dim s as JobState s = val(x) // This fails to compile.
How do I cast an integer or string to the JobState type?[/quote]

s = CType( Val(x), JobState)