ProblemYou want to add a set of related constants to your project and establish variables based on that set of constants. SolutionAdd an enumeration to your namespace, class, or other type using the Enum statement. Then use the name of the enumeration just as you would any other .NET integral data type. DiscussionEnum lets you build a list of related integer values: Enum StorageMedia Floppy CD DVD FlashRAM Paper End Enum In this enumeration, all elements are of type Integer, with values ranging from 0 (Floppy) to 4 (Paper). You can select a different type through an As clause, and you can indicate specific numeric values: Enum StorageMedia As Short Floppy = 100 CD DVD FlashRAM Paper = 500 End Enum After you've created your enumeration, refer to individual members by combining the enumeration name and the member name: storageType = StorageMedia.FlashRAM Creating variables of an enumeration type is just as simple: Dim storageType As StorageMedia Although storageType might act like a Short or Integer (as defined through the underlying Enum statement), it is truly a variable of type StorageMedia, a new data type all its own. Without enumerations, the only way to create a related set of integer values is to define multiple constants and trust yourself to use them as a set. Enumerations bundle like elements, making it easier to keep track of the relationships. Visual Studio also picks up on this relationship, using enumerations to enhance IntelliSense, as shown in Figure 3-5. Figure 3-5. Using IntelliSense with enumerationsAlthough enumeration variables are typed to the specific Enum, Visual Basic allows you to assign any numeric values (limited to the underlying type of the Enum) to those variables. For instance, Visual Basic doesn't stop you from assigning the value 700 to the storageType variable, even though none of the StorageMedia enumeration members have a value of 700. |