Flags Statement

Declares a set of flags for bitwise operations.


Flags flagsName Is typeName
    memberName [ = initializer ]
End Flags


Required identifier for the bit field
Required primitive integer data type or a different flagsName. Determines the size and default starting value of the set of flags.
Required identifier
Optional constant integer expression
End Flags
Terminates the Flags block.


The Flags statement permits the efficient definition of a set of flags, also known as a bit field. A flag is an integer value with just a single bit set. A bit field is a set of flags where each flag represents a Boolean value.

The Flags statement closely resembles the Enum statement, but has important differences. Whereas enumerations can only be assigned and compared, flags can be manipulated using the logical operators: And, Or, Xor, and Not.

Each uninitialized member is given the value that corresponds to the least significant bit not used at that point. The first member thus has a default value of one, the second is 2, the third is 4, the fourth is 8, etc.

A typeName is commonly one of the primitive integer types: Tiny, Byte, Short, UShort, Integer, UInteger, Int, UInt, Long, ULong, Huge, or UHuge.

A member can be given any value in the range of typeName. Attempting to initialize a member with a value outside of the range results in a compiler error. Members can have duplicate values; however, the names within a set of flags must be unique.

If typeName is the name of another bit field, the current statement represents an extension to the flags. The range of an extension is the same as its base.


Listing 1
Flags days As Byte
End Flags

weekend = days.Sat Or days.Sun

See also