?
Solved

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

Posted on 2006-06-28
1
Medium Priority
?
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

719 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