0

How to make [Flags] enum and switch case work together? Very desirable to make it looks simple. Similar Questions asked many times, but never directly to [Flags] enum.

If M1 set execute operation 1,

if M2 set execute operation2 ,

if BOTH M1 and M2 then set execute operation 1 and 2.

 [Flags]
    public enum TST
    {
        M1 =1,
        M2 =2,
        M3 =4
    }

 public void PseudoCode()
    {

        TST t1 = TST.M1 | TST.M3; //1+2= 3

        switch( t1)
        {
            case TST.M1:
                {
                    //Do work if Bit 1 set
                }
            case TST.M2:
                {
                    //Do work if Bit 2 set
                }
            case TST.M3:
                {
                    //Do work if Bit 3 set
                }
            default:
                {
                    //nothing set;
                    break;
                }
        }

    }

I also quite sure a lot of people want to know how to make it work. Request fix of C#?

justromagod
  • 933
  • 9
  • 20
  • You can easily do this but you have to add all the unique combinations yourself. A switch picks 1 case, it doesn't pick N matching cases, that's the purpose of a switch so you're going to have to use a different method (if-statements) or just live with how it works. – Lasse V. Karlsen Sep 12 '18 at 08:59
  • 2
    And just to be clear, this is not a bug, so this won't get "fixed". This is 100% by design and will not be changed the way you want it to behave. – Lasse V. Karlsen Sep 12 '18 at 09:22

2 Answers2

3

This will execute for any bit set

for example

ExecuteOnFlagValue(TST.M1 | TST.M3); //1+2= 3

Will execute code for bits 1 and 3

public void ExecuteOnFlagValue(TST value) {
    if (value & TST.M1 == TST.M1) {
        //Do work if bit 1
    }
    if (value & TST.M2 == TST.M2) {
        //Do work if bit 2
    }
    if (value & TST.M3 == TST.M3) {
        //Do work if bit 3
    }
}
mnieto
  • 3,744
  • 4
  • 21
  • 37
  • @justromagod, if you find that the response works, please mark it as accepted, so others will be confident that it works – mnieto Sep 12 '18 at 10:27
0

You can't do that with switch in c#, since once a case is executed, no other case will be executed.
You can think of a switch statement as a shorter way to write if... else if... else... constructs - where each case is an if, and the default is the final else:

This means that the following switch statement:

switch( t1)
{
    case TST.M1:
        //Do work if Bit 1 set
        break;
    case TST.M2:
        //Do work if Bit 2 set
        break;
    case TST.M3:
        //Do work if Bit 3 set
        break;
    default:
        //nothing set;
        break;
}

Is equivalent to the following if...else if... else construct:

if(t1 == TST.M1)
{
    //Do work if only Bit 1 set
}
else if (t1 == TST.M2)
{
    //Do work if only Bit 2 set
}
else if (t1 == TST.M3)
{
    //Do work if only Bit 3 set
}
else
{
    //nothing set or multiple bits set
}
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • In case of TST.M1 | TST.M2 your code will not execute the part corresponding with bit 3, becase the else if structure. Also the comparation t1 == TSM.whatever will fai because t1 is a flag enum with 2 bits set – mnieto Sep 12 '18 at 09:01
  • This does not work, the first `if`-condition is *only* true if t1 is **exactly** TST.M1, if t1 is `TST.M1 | TST.M2`, the condition will not evaluate true. – ikkentim Sep 12 '18 at 09:03
  • @ikkentim That is exactly my point. This is a demonstraiting why the switch can't work like the OP wants it to. – Zohar Peled Sep 12 '18 at 09:04
  • Might want to adjust your answer then, since this is not clear in your answer, all you're saying is that multiple cases won't run. Nothing about the difference between checking bits vs checking exact value – ikkentim Sep 12 '18 at 09:05
  • Yes, well, I was rudely interrupted before completing the edits I wanted to do on this answer... never mind, the answer by mnieto already covered the rest. – Zohar Peled Sep 12 '18 at 10:31