Export (0) Print
Expand All

Visual Basic Concepts

Visual Studio 6.0

Using Enumerations to Work with Sets of Constants

Enumerations provide a convenient way to work with sets of related constants and to associate constant values with names. For example, you can declare an enumeration for a set of integer constants associated with the days of the week, then use the names of the days in code rather than their integer values.

You create an enumeration by declaring an enumeration type with the Enum statement in the Declarations section of a standard module or a public class module. Enumeration types can be declared as Private or Public with the appropriate keyword. For example:

Private Enum MyEnum

-or-

Public Enum MyEnum

By default, the first constant in an enumeration is initialized to the value 0, and subsequent constants are initialized to the value of one more that the previous constant. For example the following enumeration, Days, contains a constant named Sunday with the value 0, a constant named Monday with the value 1, a constant named Tuesday with the value of 2, and so on.

Public Enum Days
   Sunday
   Monday
   Tuesday
   Wednesday
   Thursday
   Friday
   Saturday
End Enum

Tip   Visual Basic provides a built-in enumeration, vbDayOfWeek, containing constants for the days of the week. To view the enumeration's predefined constants, type vbDayOfWeek in the code window, followed by a period. Visual Basic automatically displays a list of the enumeration's constants.

You can explicitly assign values to constants in an enumeration by using an assignment statement. You can assign any long integer value, including negative numbers. For example you may want constants with values less than 0 to represent error conditions.

In the following enumeration, the constant Invalid is explicitly assigned the value –1, and the constant Sunday is assigned the value 0. Because it is the first constant in the enumeration, Saturday is also initialized to the value 0. Monday's value is 1 (one more than the value of Sunday), Tuesday's value is 2, and so on.

Public Enum WorkDays
   Saturday
   Sunday = 0
   Monday
   Tuesday
   Wednesday
   Thursday
   Friday
   Invalid = -1
End Enum

Note   Visual Basic treats constant values in an enumeration as long integers. If you assign a floating-point value to a constant in an enumeration, Visual Basic rounds the value to the nearest long integer.

By organizing sets of related constants in enumerations, you can use the same constant names in different contexts. For example, you can use the same names for the weekday constants in the Days and WorkDays enumerations.

To avoid ambiguous references when you refer to an individual constant, qualify the constant name with its enumeration. The following code refers to the Saturday constants in the Days and WorkDays enumerations, displaying their different values in the Immediate window.

Debug.Print "Days.Saturday = " & Days.Saturday
Debug.Print "WorkDays.Saturday = " & WorkDays.Saturday

You can also use the value of a constant in one enumeration when you assign the value of a constant in a second enumeration. For example, the following declaration for the WorkDays enumeration is equivalent to the previous declaration.

Public Enum WorkDays
   Sunday = 0
   Monday
   Tuesday
   Wednesday
   Thursday
   Friday
   Saturday = Days.Saturday - 6
   Invalid = -1
End Enum

After you declare an enumeration type, you can declare a variable of that type, then use the variable to store the values of enumeration's constants. The following code uses a variable of the WorkDays type to store integer values associated with the constants in the WorkDays enumeration.

Dim MyDay As WorkDays
MyDay = Saturday         ' Saturday evaluates to 0.
If MyDay < Monday Then   ' Monday evaluates to 1,
                        ' so Visual Basic displays
                        ' a message box.
   MsgBox "It's the weekend. Invalid work day!"
End If

Note that when you type the second line of code in the example in the code window, Visual Basic automatically displays the WorkDays enumeration's constants in the Auto List Members list.

Figure 8.7   Visual Basic automatically displays an enumeration's constants

Because the constant Sunday also evaluates to 0, Visual Basic also displays the message box if you replace "Saturday" with "Sunday" in the second line of the example:

MyDay = Sunday         ' Sunday also evaluates to 0.

Note   Although you normally assign only enumeration constant values to a variable declared as an enumeration type, you can assign any long integer value to the variable. Visual Basic will not generate an error if you assign a value to the variable that isn't associated with one of the enumeration's constants.

For More Information   See "Enum Statement." Also see "Providing Named Constants for Your Component" in "Creating ActiveX Components" in the Component Tools Guide, available in the Professional and Enterprise editions.

Show:
© 2014 Microsoft