471,347 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting the number of items in a enum

UJ
Is there a way to get the number of items that are in an enum? I'm trying to
find out how many items there are so that I can hit every one of them to
test.

TIA - Jeff.
Feb 1 '06 #1
4 2184
UJ wrote:
Is there a way to get the number of items that are in an enum? I'm trying to
find out how many items there are so that I can hit every one of them to
test.


Look at Enum.GetValues and Enum.GetNames.

Jon

Feb 1 '06 #2
> Is there a way to get the number of items that are in an enum? I'm
trying to find out how many items there are so that I can hit every
one of them to test.

TIA - Jeff.


Look at Enum.GetValues/GetNames

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Feb 1 '06 #3
> Is there a way to get the number of items that are in an enum? I'm trying to
find out how many items there are so that I can hit every one of them to
test.

TIA - Jeff.


enum Colors { Red, Green, Blue, Yellow };

foreach(string s in Enum.GetNames(typeof(Colors)))
Console.WriteLine(s);

There is also a GetValues(..)

Hans Kesting
Feb 1 '06 #4

"UJ" <fr**@nowhere.com> wrote in message
news:O3**************@TK2MSFTNGP14.phx.gbl...
Is there a way to get the number of items that are in an enum? I'm trying
to find out how many items there are so that I can hit every one of them
to test.

TIA - Jeff.


Enum.GetNames/GetValues is necessary as everyone hs said but might not be
sufficient because:

1) Duplicate values are allowed. This isn't really a problem but you might
want to weed out duplicate values.

2) The enum might have the FlagsAttribute in which case you need to OR
together all possible combinations.

Feb 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rene | last post: by
1 post views Thread by MLM450 | last post: by
12 posts views Thread by Mike Smith | last post: by
reply views Thread by Ronak mishra | last post: by

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.