Visual Basic Concepts
Enumerations provide an easy way to define a set of related named constants. For example, the built-in enumeration VbDayOfWeek contains numeric constants with the names vbMonday, vbTuesday, and so on.
You can use an enumeration as the data type of a property or method argument, as in the following example:
Private mdowDayOfWeek As VbDayOfWeek
Property Get DayOfWeek() As VbDayOfWeek
DayOfWeek = mdowDayOfWeek
End Property
Property Let DayOfWeek(ByVal NewDay As VbDayOfWeek)
If (NewDay < vbUseSystemDayOfWeek) _
Or (NewDay < vbSaturday) Then
Err.Raise Number:=31013, _
Description:="Invalid day of week"
Else
DayOfWeek = mdowDayOfWeek
End If
End Property
When users of your component enter code that assigns a value to this property, the Auto List Members feature will offer a drop down containing the members of the enumeration, as shown in Figure 6.3.
Figure 6.3 Auto List Members displays enumerations
Tip You might think that you could save space by declaring the internal variable mdowDayOfWeek
As Byte instead of As VbDayOfWeek — since the latter effectively makes the variable a Long. However, on 32-bit operating systems the code to load a Long is faster and more compact than the code to load shorter data types. Not only could the extra code exceed the space saved, but there might not be any space saved to begin with — because of alignment requirements for modules and data.
You can make the members of an enumeration available to users of your component by marking the enumeration Public and including it in any public module that defines a class — that is, a class module, UserControl, or UserDocument.
When you compile your component, the enumeration will be added to the type library. Object browsers will show both the enumeration and its individual members.
Note Although an enumeration must appear in a module that defines a class, it always has global scope in the type library. It is not limited to, or associated in any other way with the class in which you declared it.
The members of an enumeration need not be sequential or contiguous. Thus, if you have some general-purpose numeric constants you wish to define for your component, you can put them into a catch-all Enum.
Public Enum General
levsFeetInAMile = 5280
levsIgnitionTemp = 451
levsAnswer = 42
End Enum
In the preceding code example, both the Enum and its members were prefixed with four lowercase characters chosen to identify the component they belong to, and to reduce the chance that users of the component will encounter name conflicts. This is one of the general naming rules discussed in "What’s in a Name?" earlier in this chapter.
For More Information Enumerations are discussed in detail in "More About Programming" and "Programming with Objects," in the Visual Basic Programmer’s Guide.
The members of an Enum can have any value that fits in a Long. That is, they can assume any integer value from -2,147,483,648 to 2,147,483,647. When you declare a variable using the name of an Enum as the data type, you’re effectively declaring the variable As Long.
Occasionally you may need to provide a string constant, or a constant that isn’t an integer value. Visual Basic doesn’t provide a mechanism for adding such values to your type library as public constants, but you can get a similar effect using a global object with read-only properties.
If your component doesn’t contain a global object, such as Application, add a public class module named GlobalConstants to your project. Set the Instancing property to GlobalMultiUse.
For each constant you want to provide, add to the GlobalConstants class module a Property Get procedure that returns the desired value. For example, the following code provides Avogadro’s Number as a constant, and mimics the vbCrLf constant in Visual Basic.
Public Property Get Avogadro() As Double
Avogadro = 6.02E+23
End Property
Public Property Get vbCrLf() As String
vbCrLf = Chr$(13) & Chr$(10)
End Property
Because the Instancing property is GlobalMultiUse, a user of the component doesn’t have to explicitly create an instance of the GlobalConstants class in order to use the constants. The constants can be used as if they were part of Visual Basic:
strNewText = "Line1" & vbCrLf & "Line2"
Note A user of Visual Basic, Microsoft Excel, or any other application that hosts Visual Basic for Applications would never see this version of the vbCrLf constant, because the VBA type library is always higher in the References dialog than the type library of any component.
For More Information Global objects are discussed in "Instancing for Classes Provided by ActiveX Components" in this chapter, and in "Building Code Components."