Enum Values As Bit Flags
Enumerations can be designed for supporting bitwise operations by marking them with the Flags
attribute. This allows combining enumeration values with bitwise operators such as And
and Or
. Consider the following implementation of the Sports
enumeration that was described previously:
<Flags>Public Enum Sports None = 0 Biking = 1 Climbing = 2 Swimming = 4 Running = 8End Enum
When you apply the Flags
attribute, enumeration values are treated as a set of bit fields. It is important to define enumeration constants in powers of two (1, 2, 4, 8, and so on) so that individual flags in combined enumeration constants do not overlap.
When you Flags
, values are evaluated in binary and can be combined with bitwise operators. ...
Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.