I have an enum that is declared in the following manner (but with more options – about 15 or so) since I wanted an easy way to define all options without having to explicitly list them.
[Flags]
enum Options
{
Option1 = 1,
Option2 = 2,
Option3 = 4,
Option4 = 8,
AllOptions = 15
}
So that instead of saying
Options foo = Options.Option1 | Options.Option2 | Options.Option3 |... ad nauseum
I can just say
Options foo = Options.AllOptions;
Is this wise? Or is there perhaps a better way to give a variable all available options without explicitly having to list them?
Well, you could use my Unconstrained Melody project:
(There are then all kinds of other operations you can do using Unconstrained Melody.)
Personally I’d try to avoid hard-coding an “all” value though – it would be all too easy to add a new value and forget to update the “all” value.
(I’d also avoid naming it
AllOptionsin this case – it should just beAll, to avoid the redundancy inOptions.AllOptions.)If you do do it, add a unit test to make sure you’ve done it right 🙂