A developer on my project has implemented the following enum
[Flags]
public enum Permissions
{
Overview = 1,
Detail = 3,
Edit = 7,
Delete = 31,
Block = 39, // Requires Edit = 7, and It's own location = 32. Therefore 7 + 32 = 39.
Unblock = 71, // Requires Edit = 7, and It's own location = 64. Therefore 7 + 64 = 71.
All = int.MaxValue
}
Now, as you can see he has, for example, made Details = 3
. The reason he has done this is that Details (which should have been 2) includes overview as well (2+1=3).
I always thought that the way to do these things is to use powers of 2 in the enum and do any oring and anding outside of the enum. What's going on here?
There's nothing wrong with defining combinations in the enum in order to make life easier for users of the enum. That said, it is probably a short-sighted decision (inflexible and hard to understand code) decision to leave out that various power-of-2 options that make up the combinations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With