Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Consider i have enumerator like this :

C#
enum ProgramDeclaration
 {
     program=1,
     identifier=2,
     leftcurly=3
 };


and to get all values via foreach i use this code :
C#
foreach (ProgramDeclaration val in Enum.GetValues(typeof(ProgramDeclaration)))
{
    MessageBox.Show(val.ToString());
}

Problem : above code only give me output :
program
identifier
leftcurly

Question : how to get the integer value 1, 2 and 3? i want to show it in messagebox?

What I have tried:

looking for any sample on MSDN but i got nothing..
Posted
Updated 4-Jun-16 3:51am
v2
Comments
Dave Kreskowiak 4-Jun-16 8:45am    
Just to clairfy and keep future questions from being confusing, that's an "enumeration", not an "enumerator".

An enumerator iterates over the items in a collection, returning each item, one by one.
Philippe Mori 4-Jun-16 8:52am    
Are you serious? You simply have to press F1 in Visual Studio and scroll down in the online help to have a sample that show you one way to do this.
Gun Gun Febrianza 4-Jun-16 8:59am    
@mori
yes i am wrong, i did not read in details. just quick reading. sorry,

@dave
thanks for correct my question title. :)

Try:
C#
foreach (ProgramDeclaration val in Enum.GetValues(typeof(ProgramDeclaration)))
{
    Console.WriteLine("Value : {0} and Integer Value : {1}", val, (int) val);    
}
 
Share this answer
 
v2
Comments
Gun Gun Febrianza 4-Jun-16 8:57am    
thx brother :D you are really codeproject angelkeeper (y)
OriginalGriff 4-Jun-16 9:20am    
Nah! I tried, but they keep escaping between the bars! :laugh:
You're welcome!
Gun Gun Febrianza 5-Jun-16 3:24am    
:laugh: :D
For sure, you won't get an int if val is declared as an enumeration and there is an item in the enumeration for that value.

If you want integer values, then get integer value! This is as simple as that!
C#
foreach (int val in Enum.GetValues(typeof(ProgramDeclaration)))
{
    MessageBox.Show(val.ToString());
}


By the way, this is well documented with samples in the documentation:
Enum.GetValues Method (Type) (System)[^]

And it would even take less time to do F1 in Visual Studio to get online help that it take to write a question like this one.
 
Share this answer
 
Comments
Gun Gun Febrianza 4-Jun-16 8:56am    
thanks! vote five star.
well i didnot see that link on msdn..
Please see my article with comprehensive analysis of enumeration types based on reflection: Enumeration Types do not Enumerate! Working around .NET and Language Limitations.

The simpler method based on GetValues does work, but there are problems you face when you define different enumeration members with identical underlying integer values. Such cases are very important in practice. My article, among other things, explains this situation in detail and provides a method independent of the assigned values.

—SA
 
Share this answer
 
Comments
BillWoodruff 4-Jun-16 14:17pm    
My vote of #4: partly to counter-act an unwarranted down-vote. While this "solution" (imho) may not be the most appropriate for this OP, it links to interesting, and valuable, content.
Gun Gun Febrianza 5-Jun-16 3:09am    
nice articles five star ! and bookmark
thanks for coming sir SA :))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900