TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Zagiel
NA
7
0
Enum elements count
Mar 29 2009 10:19 AM
Is there a way to get the amount of elements in an enum? AFAIK there was no such thing in C++ and I'm afraid neither is in C#, although I find it quite neccessary: Imagine you've got an enum and a static dictionary of enum elements mapped to f.e. strings. You fill this dictionary with values at run-time start, but you want to make sure that whenever you enlarge the enum, you won't forget to enlarge the dictionary too. So it'd be nice to have something like an assert(myDictionary.Count == myEnum.Count) performed at the end of dictionary init. The only way I manage to achieve such a self-check is to have an const int lastMyEnumElement = (int)MyEnumType.lastMyEnumElement and do an assert(myDictionary.Count == lastMyEnumElement + 1), but this is useless if you add a new enum element and forget to update lastMyEnumElement. Self-updating MyEnumType.Count would solve the problem. Is there any other way?
Reply
Answers (
3
)
Padding is invalid and cannot be removed
How to set the messagebox background color