How to iterate through a collection of enum values?

B

Billy Porter

Greetings,

If I populate an ArrayList with values from, let's say, the Keys enum:

ArrayList a = new ArrayList();
a.Add(Keys.Home);
a.Add(Keys.End);

....then how do I iterate through the collection to see which values it
contains?

Thanks,
Billy
 
A

Arne Janning

Billy said:
If I populate an ArrayList with values from, let's say, the Keys enum:

ArrayList a = new ArrayList();
a.Add(Keys.Home);
a.Add(Keys.End);

...then how do I iterate through the collection to see which values it
contains?

Hi Billy,

string[] allKeys = System.Enum.GetNames(Keys.GetType());
foreach (string s in allKeys)
{
Console.WriteLine(s);
}

Cheers

Arne Janning
 
A

Arne Janning

Arne said:
Billy said:
If I populate an ArrayList with values from, let's say, the Keys enum:

ArrayList a = new ArrayList();
a.Add(Keys.Home);
a.Add(Keys.End);

...then how do I iterate through the collection to see which values it
contains?


Hi Billy,

string[] allKeys = System.Enum.GetNames(Keys.GetType());
foreach (string s in allKeys)
{
Console.WriteLine(s);
}

Cheers

Arne Janning

Or did you mean something else:

ArrayList al = new ArrayList();
al.Add(DialogResult.OK);
al.Add(DialogResult.No);

foreach (object o in al)
{
Console.WriteLine(System.Enum.Parse(DialogResult.GetType(),
o.ToString()).ToString());
}

Cheers

Arne Janning
 
B

Billy Porter

Thanks!

Arne Janning said:
Billy said:
If I populate an ArrayList with values from, let's say, the Keys enum:

ArrayList a = new ArrayList();
a.Add(Keys.Home);
a.Add(Keys.End);

...then how do I iterate through the collection to see which values it
contains?

Hi Billy,

string[] allKeys = System.Enum.GetNames(Keys.GetType());
foreach (string s in allKeys)
{
Console.WriteLine(s);
}

Cheers

Arne Janning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top