0

I have situation like this:

got enum

enum MyType{
    None = 0,
    First = 1,
    Second = 2,
    Third = 4,
    //and so on 
}

now I wan't to check if given number is result of the sum of the binary for example

int givenNumber = 3;
int result = 
//how to get that it's sum of 2 enums in this particular case would be (int)Type.First & (int)Type.Second == 3;

Hope that my question is clear enough and someone can help with this.

Edit

  1. enum values are always powered by 2

  2. I want to write a method which should check that given number can be one pice of different binary sum example:int givenNumber = 1; //1 , 3 ,5 and 7 will be the answer

  3. Real life sample: I got contractors in my db and every contractor can be Producer = 1,Supplier = 2,Service = 4 in db I can store only 1 value int in this case would be smartest choice. When I loading Items to my combobox of Service - Contractors I want load every row which have for example value 4 or 5 or 7 (beacuse 7 = 1+2+4, 5 = 1+4 and 4=4).

Is now my question more clear?

Harry89pl
  • 2,415
  • 12
  • 42
  • 63

4 Answers4

5

You can get the individual enums like this:

static IEnumerable<MyEnum> GetFlags(this MyEnum e)
{
    var flags = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
    foreach (var flag in flags)
        if ((flag & e) > 0) yield return flag;
}

then you can use it like this:

int givenNumber = 3;
var results = ((MyEnum)givenNumber).GetFlags();
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • 2
    Neat! I was also going to suggest GetValues but missed the point on foreach - I've (re)learned something today! – CHill60 Feb 08 '13 at 13:43
1
private enum YourType
{
    None = 0,
    First = 1,
    Second = 2,
    Third = 4,
}

private static void Main(string[] args)
{
    int givenNumber = 3;
    YourType castedNumber = (YourType)givenNumber;

    if ((castedNumber & YourType.First) == YourType.First)
    {
        Console.WriteLine("First");
    }

    if ((castedNumber & YourType.Second) == YourType.Second)
    {
        Console.WriteLine("Second");
    }

    if ((castedNumber & YourType.Third) == YourType.Third)
    {
        Console.WriteLine("Third");
    }

    Console.Read();
}

Do note that:

  1. This won't work for None (0) value, so you have to manually check for 0 (as (x & 0) is always 0)
  2. All values must be power of 2
  3. You might want to add the [Flag] attribute so other developers will now the enum will works with bitwise operations
ken2k
  • 48,145
  • 10
  • 116
  • 176
0
    enum Flag
    {
        None = 0,
        First = 1,
        Second = 2,
        Third = 4,
        Fourth = 8
    }

    static void Main(string[] args)
    {
        int givenNumber = 3;
        List<Flag> flags = new List<Flag>();

        int number, result;
        do
        {
            int step = 0;
            do
            {
                number = (int)Math.Pow(2d, ++step);
            } while (number < givenNumber);
            result = (int)Math.Pow(2d, step - 1);
            flags.Add((Flag)result);
            givenNumber -= result;
        } while (givenNumber != 0);

        Debugger.Break();
    }
Frederiek
  • 1,605
  • 2
  • 17
  • 32
0
[Flags]
enum MyType{
  None = 0,
  First = 1,
  Second = 2,
  Third = 4,
  //and so on 
}

Using Flags attribute means a simple toString will give you what you want for your dropdown

i.e.

MyType test = (MyType)6;
Console.WriteLine(test.ToString());

would now print:

Second, Third

instead of 6

KingCronus
  • 4,509
  • 1
  • 24
  • 49