Enums

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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;
 
=?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/
 
Back
Top