Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Looking for a better way to loop through an Enum with flagsattribute

Posted on 2006-06-28
1
Medium Priority
?
288 Views
Last Modified: 2008-02-01
I have an enum with flagsattribute called ModuleType and I want to be able to loop through it.  I am currently turning it into a string, and looping through the results as follows:

      ModuleType mt = ModuleType.Option1 | ModuleType.Option2 | ModuleType.Option3;

      string[] e = mt.ToString().Split(',');

      foreach(string s in e)
      {
            ModuleType m = (ModuleType)Enum.Parse(typeof(ModuleType),s.Trim());
            // Do work
      }

Can anyone recommend a better or more elegant way of looping through the enumerator without having to turn them into a string and then back to an enumerator again?

Thanks
0
Comment
Question by:Ashwin_shastry
1 Comment
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 375 total points
ID: 17002496
Yes you can use & to see if the flag is set.

This can easily be drawn into more generic code by simply reading the metadata on the enum itself (i.e. GetValues)

    class Program {
        [Flags]
        enum foo {
            Option1 = 0x0001,
            Option2 = 0x0002,
            Option3 = 0x0004,
            Option4 = 0x0008
        }
        static void Main(string[] args) {
            foo f = foo.Option1 | foo.Option2 | foo.Option3;
            foreach(int value in Enum.GetValues(typeof(foo))) {
                if (((int) f & value) > 0) {
                    Console.WriteLine(Enum.GetName(typeof(foo), value) + " set");
                } else {
                    Console.WriteLine(Enum.GetName(typeof(foo), value) + " not set");
                }
            }
        }
    }

Although generally you should be checking a value against a single options .. i.e. you are interested in whether or not f has Option1 set in which case you would f & foo.Option1 > 0

Cheers,

Greg
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question