Pearson Software Consulting Services
Using ENUM Type Variables In VBA
Enum variables are a special, self-documenting type of
variables that you can use in your VBA code. You use Enums to group several
values of a variable into a single variable declaration. All Enum variables
are of type Long. An example will serve well to illustrate the use of Enums.
Imagine that you have 3 types of employees: contract, hourly, and salaried.
To assign constant values to these employees, you might be tempted to use
Const statements, as follows.
Const Contract As Long = 0 You can then use those constants in code to assign a type to a variable. E.g., Dim EmployeeType As Long Enum EmpType Dim EmployeeType As EmpType One advantage of doing so is that the code is self-documenting. You can tell that the EmployeeType variable can take on the values listed in the Enum EmpType. Moreover, you get VBA"s Intellisense support when you assign a value to the EmployeeType variable, as show below:
It should be noted that any Long number may be
assigned to EmployeeType. Just because only 3 named values exist in the Enum
type, you are not limited to those values. The compiler will not warn you if
you assign a value not in the list to the variable, and no runtime error is
raised. Enum EmpType Enum EmpType You can assign a value to one of the elements, and the compiler will begin renumbering from that element through the last element. For example the following are functionally equivalent Enum EmpType Enum EmpType
|
|
|
|
||
Created By Chip Pearson and
Pearson Software Consulting, LLC
This Page:
Updated: November 06, 2013
MAIN PAGE
About This Site
Consulting
Downloads
Page Index
Search
Topic Index
What's New
Links
Legalese And Disclaimers
chip@cpearson.com
© Copyright 1997-2007 Charles H. Pearson