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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.204.216