Enumerations are similar to constants in that they are named entities that are assigned values. However, enumerations behave like groups of public numeric constants in a module or class. An enumeration is treated as a custom data type, and you can use it to create constants of suitable values for variables, parameters, and properties. You might already be using enumerations in Visual Basic .NET. For instance, when you call MessageBox.Show() to display a message box, the IntelliSense feature in Visual Basic .NET displays a drop-down list for the buttons parameter. (See Figure 4-1.)
The time that the developers of the MessageBox.Show() method invested to provide the buttons parameter values in an enumeration pays you dividends as a programmer; you never have to remember the numeric values of the parameter, and the chance of incorrectly specifying a value is greatly diminished.
Note
With Option Strict turned off, Visual Basic .NET allows you to specify a number in place of an enumerated value. Of course, doing so would create a magic number which is bad. To actually use a magic number when an associated member name is available is just shy of insane. When you turn Option Strict on (which I highly recommend), Visual Basic .NET forces you to specify a valid member of the enumeration.
You create enumerations much like you do structures. In the Declarations section of a module or class, you type the word Public or Private, then Enum, then the name of your custom enumeration, and then the data type of the enumeration. Enumerations must be one of the following data types: Byte, Integer, Long, or Short. If you don't specify a data type, the enumeration is automatically created as an Integer.
The following is a sample enumeration:
Public Enum CustomBorderStyle As Byte None = 0 Raised_Light = 1 Raised_Heavy = 2 Sunken_Light = 3 Sunken_Heavy = 4 End Enum
This enumeration creates an enumerated type with five values. Although you always refer to the name of an enumeration member when you write code, the name simply represents its numeric value, much like a constant.
Once you've created an enumeration, you can use it as the data type for any variable, parameter, Function procedure, or Property procedure within the scope of the enumeration. For instance, to create a CustomBorderStyle property that uses the enumerated type shown previously, you could declare a procedure like this:
Public Property BorderStyle() As CustomBorderStyle Get End Get Set(ByVal Value As CustomBorderStyle) End Set End Property
When you reference this property, as well as when you reference Value in the Set section, Visual Basic .NET displays an IntelliSense drop-down list with all the enumeration members, as shown in Figure 4-2.
If you turn on Option Strict, simply declaring a parameter as an enumerated type guarantees that the value passed to the parameter will be one of the defined enumeration members (or a combination of members, as you'll see). However, when Option Strict is turned off, a user can specify any numeric value even one that doesn't have a corresponding enumeration member. As a matter of fact, this is one of the most common misconceptions about enumerated types. This is just one more reason why turning Option Strict on is such a great idea.
Goals of Using Constants and EnumerationsThe goals of using constants and enumerations include
|