Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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