complete-reference-vb_net_15
complete-reference-vb_net_15
Note Parse is a very useful method for converting the String representation of a numeric type into
a numeric type.
The last method to look at is one you will use frequentlyIsDefined. As mentioned in Table 8−1, this method
verifies that the argument specified as a particular constant actually exists in the enumeration. You get True
or False depending on whether the type is defined or not. Here is an example:
The following code declares an enumeration for warp speeds (so there can be no errors when choosing):
We can then test if the helmsperson has entered the right values, as shown here, so that we do not rocket away
to infinity:
If [Enum].IsDefined(GetType(WarpFactorEnum), _
"ImpulsePlusTen") Then
Throw New ArgumentOutOfRangeException()
End If
Flags
We typically use enumerations for lists of named constants, such as days of the week, spectrum of colors, and
stars in a solar system; on the other hand, we generally rely on bit fields to combine qualities or quantities of
these constants. Here's an example in code:
In Chapter 5 we discussed bit flags and bit maps in the context of operators, and now we'll examine them in
the context of enumerations. The framework employs the Enum type for various purposes, and one of them is
to represent collections of bit flags. You'll see a lot of these in the System.Forms namespace, as discussed in
Chapter 16.
Using the WinCV tool, you can browse for many enumerations that encapsulate collections of bit states.
These include enumerations for visual states, appearances, the state of controls, forms, tables, databases, file
attributes, and button shapes. Figure 8−1 illustrates the framework's enumeration for database table columns
(they are all in C#).
253