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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

about enum 3
Enum TypeConverter 3
Enum Extentions 7
enum with underlying type 1
Casting integers to generic parameters 3
Merge Info From Two Enums 1
Check Flags Enum for Validity 15
enum type 3

Back
Top