Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
enum abcd{read}
enum read{view,edit}
method
{
  abcd opname=(abcd)1;
  
}
foreach(var i in Enum.GetValues(typeOf(opname)))
{
  ........
}
It shows an error in opname.
I'm not able to do.

So please give me a suggestion.
Posted
Updated 31-Jan-14 8:37am
v2

C#
foreach(var i in Enum.GetValues(typeof(abcd))) {}

typeof(T) where T is the Enum Type Name, not the Enum Object value...
 
Share this answer
 
begin edit #1 ...

An example of including an element of an Enum inside another Enum, and accessing the contents of the included Enum.
C#
// using these two Enums:

public enum someEnum
{
    one, two, three, four, five
}

public enum someOtherEnum
{
    someEnum, six, seven, eight
}

// inside some method ...

// note the required Cast !
var enu = (someEnum) someOtherEnum.someEnum;

foreach (var v in Enum.GetValues(enu.GetType()))
{
    Console.WriteLine(v + " value = " + Convert.ToInt32(v).ToString());
}

Note that I think using this type of code is a mistake, and that using structs, or static classes, with Constants is a better idea.

end edit #1 ...

Your code suggests to me that you are not quite clear on what Enums are, but you can iterate over the values in the Enum like this:
C#
// using this Enum
public enum someEnum
{
    one,
    two,
    three,
    four,
    five
}

// in some method ...

// casting to an Array here saves boxing and unboxing for each element
foreach(var v in Enum.GetValues(typeof(someEnum)) as someEnum[])
{
    Console.WriteLine(v + " value = " + Convert.ToInt32(v).ToString());
}
You can iterate over the names of the Enum constants like this:
C#
foreach (var v in Enum.GetNames(typeof(someEnum)))
{
    Console.WriteLine(v + " value = " + Convert.ToInt32(Enum.Parse(typeof(someEnum), v)).ToString());
}
Now, what is your goal here; what are you trying to do ?
 
Share this answer
 
v2
Comments
Member 10562368 31-Jan-14 6:29am    
First of all thank you for giving suggestion of my question.
so my question as well as I trying to do is: I have two enum's which I specified above as abcd & read.I tried to point one enum(abcd) to another(read..ie value of abcd).I just pass the value of 'abcd'(enum) in foreach ,but it couldn't took 'read' as another enum type.

so,simply I need to know how would I pass value of one enum so that I can iterate and take values of secondary enum ..is it possible ?
BillWoodruff 31-Jan-14 13:05pm    
I'll revise my answer to include a demonstration of including one Enum as an element of another Enum, but I think you are off-track here, and what you want to do can best be done with structs or classes, and the use of constants.

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