4.2. Enumerated Constants

Constants, as you know, are useful for improving the readability and maintainability of code by making it self-documenting. However, you can't define a public constant (using the Public Const statement) within a class module. How do you make some constants available both to yourself and possibly to other users of your class? The answer lies in the use of enumerated constants.

Enumerated constants allow you to create a set of constants that become intrinsic to your application or class, very much like the intrinsic constants, such as vbCrLf and vbRightButton, within VB itself. By using enumerated constants within your class, you can associate a constant name and its value with the class, in the process providing the user of the class with a set of meaningful constants that are instantly available from the IntelliSense drop-down list for statement completion.

4.2.1. Using Enumerated Constants

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 ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.