Enums

G

Guest

Hi,

If I have this enumeration:

public enum SampleEnum
{
First=1,
Second=2,
Third=3
}

and I have a variable of type SampleEnum, how do I get its corresponding
number?
I tried this:

SampleEnum a=SampleEnum.Second;
int i=a.ConvertToInt16(a); // i=2

and it works, but I am not sure if it is the right way to do it.

Thanks
 
R

Ryan Trudelle-Schwarz

Hi,
If I have this enumeration:

public enum SampleEnum
{
First=1,
Second=2,
Third=3
}
and I have a variable of type SampleEnum, how do I get its
corresponding
number?
I tried this:
SampleEnum a=SampleEnum.Second;
int i=a.ConvertToInt16(a); // i=2
and it works, but I am not sure if it is the right way to do it.

Enums inherit from one of the simple integer types (byte, int16, int32, int64...).
If you do not specify which one it will inherit from int32. Thus, you can
do a direct cast to Int32 or any other integer type. So you could just as
easily do: int i = (int)a;
 
C

Chad Z. Hower aka Kudzu

=?Utf-8?B?Wg==?= said:
If I have this enumeration:

public enum SampleEnum
{
First=1,
Second=2,
Third=3
}

and I have a variable of type SampleEnum, how do I get its corresponding
number?
I tried this:

SampleEnum a=SampleEnum.Second;
int i=a.ConvertToInt16(a); // i=2

and it works, but I am not sure if it is the right way to do it.

int i = (int)a;


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 

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

Top