Elegabalus
Programmer
Having an odd problem with an enum I'm working with:
It seems when I cast an integer to the enum type, it is casting it by index, not by value.
I want to be able to get the integer (being pulled from a database), cast it to the enum type, and use it from there.
I've also tried the following syntax, but it does pretty much the same thing:
Feels kinda like I'm missing something simple here...any help is appreciated.
Code:
public enum EnumTest
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
int i = 2;
EnumTest val = (EnumTest)i
Result: Value3
It seems when I cast an integer to the enum type, it is casting it by index, not by value.
I want to be able to get the integer (being pulled from a database), cast it to the enum type, and use it from there.
I've also tried the following syntax, but it does pretty much the same thing:
Code:
(EnumTest)Enum.ToObject(typeof(EnumTest), i)
Feels kinda like I'm missing something simple here...any help is appreciated.