For Programmers: Free Programming Magazines  


Home > Archive > C# > March 2004 > Iterating through enums









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Iterating through enums
Doug Martin

2004-03-28, 10:29 pm

Thought that I would post this to help anyone else that encounters the
same problem. It took a little while for my C-wired brain to figure
it out in C#.

Problem: I needed to enumerate over all possible values in a very
large and growing enumeration to make sure each one was handled later
in some code.

Solution:

# a big enumeration
enum Foo { one, two, three, ..., bazillon, };

foreach (int i in Enum.GetValues(typeof(Foo)))
{
Foo fooItem = (Foo) Enum.ToObject(typeof(Foo), i);
... code to use fooItem here ...
}

Hope this helps the next person that needs to do the same.

- Doug Martin
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com