Let’s say I need both an enum both in a flags and in a non-flags variant.
-
Option 1: I could duplicate everything:
enum Color { Red, Blue, Green } [Flags] enum Colors { None = 0, Red = 1, Blue = 2, Green = 4 } // use cases Color currentColor; Colors supportedColors; -
Option 2: I could just use the Flags variant for everything:
Colors currentColor; // ugly, since neither "None" nor "Red | Blue" should be valid
I don’t like either of these: In Option 1, Color.Red, and Colors.Red are completely unrelated, which might require binding code. In addition, I’d have to keep the two enums synchronized. The drawback of Option 2 is obvious. What I’d actually like is something like
enum Colors = Flag set of Color;
Is there a more elgant solution to this requirement?
I would simply use the
[Flags]version for everything, and simply ensure in a few places that it is only a single value. You need to do that either way, because even without[Flags]the following is valid:So you need to check that the
Coloris one you were expecting anyway. The[Flags]will only help serialization/parsing.