1

I have the following enum

[Flags]
public enum Anchor
{
    None = 1,
    Top = 2, Bottom = 4,
    Left = 8, Right = 16
}

I need to test every possible combination of Top, Bottom, Left and Right but declaring all those combinations in a switch is awful.

switch (anchor)
{
    case Anchor.Left:
        Thing1();
        break;

    case Anchor.Top:
        Thing2();
        break;

    case Anchor.Left | Anchor.Top:
        Thing3();
        break;
}

This question is not a duplicate of this or this. I've tried both solutions and neither works for me because if my enum is, say Anchor.Left | Anchor.Top, Thing1() and Thing2() would be called whereas I need Thing3() to be called.

Community
  • 1
  • 1
Spans
  • 364
  • 7
  • 17
  • 2
    Just as an aside, it's unusual to have `None` in an enum defined to be anything other than 0 - in this case would you behave differently to `case Anchor.Left | Anchor.None:` to `case Anchor.Left:`? – Rowland Shaw Dec 29 '14 at 12:36
  • So basicially you want to make compiler guess what should be called. It's not possible, if you wan't completely different reactions for states combinations, you need to specify it explicit. –  Dec 29 '14 at 12:37
  • @RowlandShaw Yes I forgot to mention that. If the given enum has `Anchor.None` set in it, all other flags should be ignored and a case for `None` should be used. It's probably not part of my problem. – Spans Dec 29 '14 at 12:43
  • How is anchor variable being set/initialized? If anchor = Anchor.Left | Anchor.Top; then Thing3() is invoked as expected. – Giorgos Betsos Dec 29 '14 at 12:46
  • @GiorgosBetsos That is true, but I need cases for all 24 combinations of `Top`, `Bottom`, `Left` and `Right` (`None` ignored). Like I said previously, declaring all those in the switch is awful. – Spans Dec 29 '14 at 12:52
  • If any combination that has `None` in it is going to be ignored, I'd put the entire `switch` statement into an `if` statement that will ignore that. Make it explicit so that your code will self-document. But a comment would also be helpful there. – krillgar Dec 29 '14 at 13:25

1 Answers1

0

Declare all these combinations in a switch. (as you started)

Its clear, understandable, maintainable.

Not awful. (Feels just a bit stupid to key it in)

After all, you have these distict cases, so a switch is the perfect construction.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • 1
    Spans, Unlike DrKoch, I'll give you an answer that is not "Jerk like". The best solution to your answer is to use "if" blocks for each flag your testing. Using a 'switch' is for when you want to target specific combinations of your flags. For example: if (anchor.HasFlag(Anchor.Left)) { Thing1(); } if (anchor.HasFlag(Anchor.Top)) { Thing2(); }, ect, ect. Notice I did not use an 'if else' as that would preclude your other flags. It is not as elegant as a "switch" statement, but it is the only solution to what your trying to achieve. Take care and God bless. – RashadRivera Mar 17 '19 at 03:18
  • @rashadrivera your answer is the best by far. I have this situation with an enum with 15 elements. Going case by case would be so tedious and unmaintainable. – Weenhallo Aug 02 '22 at 12:34