Solved

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

Posted on 2006-06-28
1
281 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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now