Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enum Problem 1

Status
Not open for further replies.

Elegabalus

Programmer
Jan 13, 2005
71
CA
Having an odd problem with an enum I'm working with:

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.
 
Have you looked at Enum.Parse()?
Code:
EnumTest myEnumtest = (EnumTest)Enum.Parse(typeof(EnumTest), i.ToString());
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top