Flags
Figure 8−1: Using WinCV to look at the built−in enumerations
Using the Enum type, we can similarly represent a collection of bit flags for any application. Looking at the
enumeration shown in Figure 8−1, one can conclude the following: an expression that checks for IsUnique
and IsKey seems perfectly feasible, as does one that verifies whether a file IsCompressed | IsEncrypted or
IsHidden AND IsReadOnly with the FileAttributes enumeration.
We can do the same for the message flag constants demonstrated in the GetMessages example listed in
Chapter 5. Remember the following collection of flags:
Dim messageFlag As Integer = 2
Dim isAccessed As Integer = 4
Dim isArchived As Integer = 8
Dim isDeleted As Integer = 16
Dim newMessage As Integer = 32
This collection would be better represented inside an enumeration. We can call the enumeration
MessageFlagEnum, as depicted in this code:
Public Enum MessageFlagEnum
MessageFlag = 0x0001
IsAccessed = 0x0002
IsArchived = 0x0004
IsDeleted = 0x0008
NewMessage = 0x0016
End Enum
We don't have to initialize the symbols with hex values, although it doesn't hurt, and the symbol values can
still be easily stored in the database. We also don't necessarily need to represent the bit flags in any special
sequencinghere I have raised each bit flag to the power of two.
However, as you have seen, the Enum semantics don't typically lend themselves to bit−flag semantics, in
which it's normal to write an expression evaluating the combined state of more than one symbol. So, when
you execute the following code,
Dim MsgState As MessageFlagEnum.IsAccessed | MessageFlagEnum.IsArchived
Debug.WriteLine(MsgState.ToString())
you are going to get the value 0x0006 to the console instead of the combination of the two symbols. Thus, if
the intention is to write "IsAccessed, IsArchived" to the Debug window, it will not happen. To force the
enumeration to return the latter, you can pass the "F" argument listed in Table 8−2 to the Format method or
254