By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,335 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,335 IT Pros & Developers. It's quick & easy.

Number of members in an Enum

P: n/a
Jay
I have a few enums, a shortened example of one is:

enum ParamTest {Err, Start, Block, Hold}

Is there a programmatic way to find the number of "members" (probably the wrong C# term) of the
Enum - this is 4 for the example given.

One not very good way that I currently use is to add a dummy "LENGTH" member on the end:

enum ParamTest {Err, Start, Block, Hold, LENGTH}

and to always remember not to assign values to the members, and to keep LENGTH at the end of the
enum if any extra members are added. That way, I can use ParamTest.LENGTH to find the number of
members (excluding the dummy one). This is messy. Is there a better way?
Jan 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi

Look at the "Enum" method. One such way of doing what you want is this:

string[] strEnumArry = Enum.GetNames(myEnum); //returns your enum into a
string array

int enumLen = strEnumArry.GetLength(); //gets you the length of that
array

"Jay" <nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>I have a few enums, a shortened example of one is:

enum ParamTest {Err, Start, Block, Hold}

Is there a programmatic way to find the number of "members" (probably the
wrong C# term) of the
Enum - this is 4 for the example given.

One not very good way that I currently use is to add a dummy "LENGTH"
member on the end:

enum ParamTest {Err, Start, Block, Hold, LENGTH}

and to always remember not to assign values to the members, and to keep
LENGTH at the end of the
enum if any extra members are added. That way, I can use ParamTest.LENGTH
to find the number of
members (excluding the dummy one). This is messy. Is there a better way?


Jan 26 '07 #2

P: n/a
Jay
Thank Daniel, that's a good idea. I need the string array anyway.

I have another question on enums, but it's not really related, so I think I'll start a new post.

"Daniel" <no****@pokercat.co.ukwrote in message news:O2**************@TK2MSFTNGP05.phx.gbl...
Hi

Look at the "Enum" method. One such way of doing what you want is this:

string[] strEnumArry = Enum.GetNames(myEnum); //returns your enum into a
string array

int enumLen = strEnumArry.GetLength(); //gets you the length of that
array

"Jay" <nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>I have a few enums, a shortened example of one is:

enum ParamTest {Err, Start, Block, Hold}

Is there a programmatic way to find the number of "members" (probably the
wrong C# term) of the
Enum - this is 4 for the example given.

One not very good way that I currently use is to add a dummy "LENGTH"
member on the end:

enum ParamTest {Err, Start, Block, Hold, LENGTH}

and to always remember not to assign values to the members, and to keep
LENGTH at the end of the
enum if any extra members are added. That way, I can use ParamTest.LENGTH
to find the number of
members (excluding the dummy one). This is messy. Is there a better way?



Jan 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.