Get the enumeration entry by integer value

Today I struggled a bit with enumerations and saving and loading them to a database. Because I didn’t found any hint in the docs and the forum, I created a feature request in order to get a method to retrieve the enumeration entry by the integer value. This could avoid endless select case structures and forgetting to enhance them when the enumeration is extended.

Guess what? There is a way to do this. Thanks @Greg_O_Lone for the perfect hint.

With CType you can convert an Integer to an enumeration entry. I didn’t found that in the docs and the forum so I just want to share that little trick:

Var x As myEnum = CType(1, myEnum) 

Replace the digit 1 with any integer value you like, so when loading from a database it is just so simple:

Var x As myEnum = CType(lRow.ColumnAt(0).IntegerValue, myEnum) 

I would be happy to find this in the documentation. As far as I could see, this is nowhere listed in the docs, so at least it is listed here now. I hope it will be useful for others too.

1 Like

You can also do this:

var x as myEnum = myEnum( 1 )
1 Like

This is also not really documented, is it?

And I think it looks more readable too.

It is now. :slight_smile:

Wow, that’s great… :+1: