Named Arguments No
[Public | Private] Enum name membername [= constantexpression] membername [= constantexpression]
End Enum name
The name of the enumerated data type.
The name of a member of the enumerated data type.
constantexpression Use: Optional
Data Type: Long
The value to be assigned to membername. Description
Defines an enumerated data type. All the values of the data type are defined by the instances of membername.
246 Chapter 7- The Language Reference
• The Enum statement can appear only at module level, in the declarations section of a form, code module, or class module.
• The Public keyword makes the enumerated data type visible throughout the project in which it's used; this is the default behavior. Note that adding a reference to a library containing public enumerated types to a project makes those types visible to the project. A class doesn't have to be instantiated to access these enumerated types.
• The Private keyword makes the enumerated data type visible only to the module in which it appears.
• constantexpression must evaluate to a Long. It can be either a negative or a positive number. It can also be another member of an enumerated data type or an expression that includes long integers and enumerated data types.
• If you assign a floating point value to constantexpression, it's rounded and converted to a long integer automatically.
• If constantexpression is omitted, the value assigned to membername is 0 if it's the first expression in the enumeration. Otherwise, its value is 1 greater than the value of the preceding membername.
• The values assigned to membername can't be modified at runtime. Programming Tips & Gotchas
• When two enumerated data types in different libraries share the same name but different members or values, references to the enumerated type are resolved based on the library that has been assigned the highest priority in the References dialog. Because enumerated data types aren't members of a class (even though they are defined in a class's type library), a reference to a particular enumerated data type can't be qualified with a class name or library name.
• Once you define an enumerated type, you can use name as the return value of a function. For example, given the enumeration:
Public Enum enQuarter enQ1 = 1 enQ2 = 2 enQ3 = 3 enQ4 = 4 End Enum you can use it as the return value of a function, as illustrated by the following function declaration:
Public Function QuarterFromDate(datVar as Date) _ As enQuarter
You can also use it in a procedure's parameter list when defining a parameter's data type, as in the following code fragment:
Public Function GetQuarterlySales(intQ As enQuarter) _ As Double
Enum Statement 247
• Individual values of an enumerated type can be used in your program just like normal constants.
• Enumerated types provide the advantage of allowing you to replace numeric values with more mnemonic labels, and of allowing you to select values using the Auto List Members feature in the VB and VBA IDEs. However, when you declare a parameter or function's return value to be an enumerated type, VBA provides no automatic type checking. Consequently, if your program needs to insure that the values passed to a routine are valid, your code is responsible for handling the validation.
• Remember that the members of an enumerated type must evaluate to a Long. This is a major limitation, and there is no workaround.
Const Statement, Select Case Statement
Was this article helpful?