Basically I want to restrict variables to the values 0, 1 or 2.
I have tried doing this with the following:
enum Value
{
0,
1,
2
};
Value var;
But this is a compile error because the enum values are unlabelled. It just makes the code less readable to assign names like “ZERO”, “ONE” and “TWO” rather than referring to the values as 0, 1 and 2. Is there any way around this or should I just get rid of the enum and enforce the rule elsewhere?
If you want to use
enum, then you need to name them. Since you’re just working with integer values, and you apparently want them to actually represent integer values, your best bet is it use anintparameter, and do a quick check at the top of the method. A comment on the method specifying this constraint would be welcome.Note that if your values actually correspond to non-numeric settings, then you should just come up with good names and use the
enum