To create a set of enumerated constants, you use the Enum statement, which defines the name of the set of constant values, the names of the individual constants within the set, and the individual values of these constants. You place the Enum statement in the declarations section of your class module. For example:
Public Enum empTypes empTypeOne = 1 empTypeTwo = 2 empTypeThree = 3 End Enum
The major drawback with enumerated constants is that their values can be numeric only. In other words, you can't declare an enumerated constant that represents a string.
Once you have created a reference to your class from the client application using the references dialog or automatically if the class module in which empTypes is defined is a part of your project, you have access to the enumerated constants via the IntelliSense drop-down list. For example, you could access the constants shown in the example above by typing emp, then pressing the Ctrl key and spacebar together; in the list of available items, you would see all three of the constants and the empTypes Enum type. This means that you can use either of the following syntactical forms:
If iType = empTypes.empTypeOne Then If iType = empTypeOne Then
Note that to use the enumerated constants from within a class, you don't have to have instantiated an object variable of that class as you do to access a property within a class. This is an important point and worth repeating. Accessing a property within a class requires you to declare an object variable of that class, then use the variable and dot notation to get to the property; for example:
Dim oVar As Employee Set oVar = New Employee OVar.Name = "Peter"
However, as soon as a class module containing an enumerated constant is included in your project or a reference to its class is added to your project, you can use those constants within your code.
For more information on the Enum statement, see its entry in Chapter 7.
Was this article helpful?