Comparing / Converting an Enum value to an Integer

Hi Folks,

I have a situation that requires the conversion and comparison of an Enum value to an integer. Is this possible? Everything that I’ve tried fails.


Dim i As Integer = CType(MyEnum.EnumMemberXXX, Int32)

Thanks, Eli. That’s it exactly. And, to finalize the thread, the reverse is also true:

[code]Dim theStudioDataType As STUDIODATATYPE // the enum

theStudioDataType = CType(theIntegerValue, STUDIODATATYPE)[/code]


Didn’t know that, thanks Tim. Just to add, it can be done without CType too

[code] dim e As MyEnum, i As integer

e = MyEnum.Foo

i = Integer(e)

e = MyEnum(i)[/code]


dim s As String = Str(e)

The docs don’t mention this.

1 Like

I just cast with ‘int32(yourEnum)’ or ‘uint32(yourEnum)’. I believe val also works.


Excellent!!! Just the info I was looking for…

The Xojo docs talk only about converting an enum to an integer, but unfortunately don’t cover how to get an enum back from an integer.

You can convert ANY integer to the enum - which is a bit of a problem as you dont get an OutOfBoundsException

Suppose you have

enum foo
     bar = 0
     baz = 1
end enum

dim i as integer = 45
dim e as foo = CType(i, foo)

when you look at E in the debugger you’ll see its highlighted in red to indicate that it is NOT one of the valid enum values