Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

20 Experts available now in Live!

Get 1:1 Help Now