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? 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?
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?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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?
|
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;
|
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...
|
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?...
|
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
|
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
|
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...
|
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
}
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |