473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Number of members in an Enum

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
2 9193
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
7
by: Jason Heyes | last post by:
I know I can do this with an enum like this: enum { a, b, c, d }; int x = a + b + c + d; But what about this one? enum X { a, b, c, d }; int x = a + b + c + d; // will this compile?
1
by: Old Wolf | last post by:
I tried this code: #include <iostream> #include <string> template<typename T> struct enum_properties { static const long max; static const std::string name;
21
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
6
by: Laszlo Szijarto | last post by:
What's the best way to recurse through the members of an enum? I want to take an enum and dump into a ListBox the enum's names. Thank you, Laszlo
1
by: Samuel Shulman | last post by:
I can't remember what is the namespace/class that can be used to retrieve all the members of an enum Thank you, Samuel
8
by: Mark P | last post by:
I'm working on a project where I have something like enum Modes {mode_a, mode_b, ...}; Due to project spec changes, the number of Modes has increased several times. There are a few places...
3
by: puzzlecracker | last post by:
What's the fastests way to find a number of values in enum. I came up with this. but I think it's over kill. enum Type { VAL1, VAL2, VAL3 }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.