C# Enum Question

  • Thread starter Thread starter Yosh
  • Start date Start date
Y

Yosh

What is the best method to convert an integer to Enum equivelant?

I hope this makes sense.

Thanks,

David
 
Yosh said:
What is the best method to convert an integer to Enum equivelant?

I hope this makes sense.

Thanks,

David

cast:

MyEnum enumvar = (MyEnum)intvar;


Hans Kesting
 
Yosh said:
What is the best method to convert an integer to Enum equivelant?

I hope this makes sense.

Thanks,

David

// Notice the type specifier, int.
public enum MyEnums : int
{
Enum1 = 0,
Enum2,
Enum3
}


...

MyEnum myVar = 1;

...

Another way:

MyEnum myVar = (MyEnums) 1;

...

If you just have the name of the enum value:

MyEnum myVar = System.Enum.Parse(typeof(MyEnums), "Enum2");

Hope this helps ;)

Mythran
 
Back
Top