Enumerations 


Enumerations (also called enumerated types) provide a way to associate meaningful names with a sequence of constant values. 

Syntax:

          [Public | Private] Enum name
                    memberName [= constantExpression]
                    memberName [= constantExpression]
                    . . .
          End Enum
 

Example -- define an enumeration for the months of the year :

          Private Enum monthsOfYear
                    January = 1
                    February = 2
                    March = 3
                    April = 4
                    May = 5
                    June = 6
                    July = 7
                    August = 8
                    September = 9
                    October = 10
                    November = 11
                    December = 12
          End Enum

The name of the enumeration appears after the keyword Enum.

The constant names (January, February, etc.) and values (1, 2, etc.) follow.

The enumeration declaration belongs in the form level declarations, and should appear before it is used to declare other variables.


Declaring Variables and Arrays Using an Enumeration Type 

Any variables related to the month number can now be declared using the name of the enumerated type.

          Dim totMonths As monthsOfYear
          Dim monthNum As monthsOfYear
 

The above variables can only be assigned the 12 values January to December.  Further, the enumerated type can be used to declare array declarations:

          Dim salesData (January To December) As Long


Using an Enumeration Type with Loops

Instead of using 1 To 12 for any loop that counts through the months, January To December can be used.  Using the declaration of monthsOfYear from above, the following code processes the monthly sales for a particular store.

          Dim salesData (January To December) As Long
          Dim storeTotal as Long
          Dim monthNum as monthsOfYear

          storeTotal = 0
          For monthNum = January to December
               storeTotal = storeTotal + salesData(monthNum)
          Next monthNum

Enumerated types contribute to self-documenting code.  The example above is clearer than the following:

          Dim salesData (1 To 12) As Long
          Dim storeTotal as Long
          Dim monthNum as Long

          storeTotal = 0
          For monthNum = 1 to 12
               storeTotal = storeTotal + salesData(monthNum)
          Next monthNum


Facts about Enumerations


An Enumeration Example using Weekday

The function Weekday converts the string "April 14, 2000" to a number representing the day of the week for that date, in this case 6.

If Appointment is of type daysOfWeek, and daysOfWeek is declared as

          Private Enum daysOfWeek
                    Sunday = 1
                    Monday
                    Tuesday
                    Wednesday
                    Thursday
                    Friday
                    Saturday
          End Enum

Dim Appointment as daysOfWeek

...then enumerated types allow statements like... 

     Appointment = Weekday(Now)
Or
     If Appointment < Wednesday then

Both of the statements above perform comparisons on the value (1 - 7) of the enumerations.

In the declaration above, only Sunday was explicitly given a value, and the rest were automatically assigned successive integers.


Printing an enumerated variable

Finally, the value of an enumerated value (1 - 7) can be printed:

         Private Sub cmdGo_Click( )
              Dim ctr As daysOfWeek

              For ctr = Sunday To Saturday
                  Print ctr
              Next ctr
         End Sub

A more useful approach, which uses the WeekdayName function provided by VB, will print the corresponding weekday name instead of 1 through 7.

         Private Sub cmdGo_Click( )
              Dim ctr As daysOfWeek

              For ctr = Sunday To Saturday
                  Print WeekdayName ( ctr )
              Next ctr
         End Sub

 

Sunday was originally initialized to 1 in order to correspond to the WeekdayName function, which associates Sunday with the value 1. 


Two Final Enumeration Examples

          Private Function salaryMultiplier (ByVal day as daysOfWeek) as Single
               Select Case day
                    Case Saturday, Sunday
                             salaryMultiplier = 1.5
                    Case Else
                             salaryMultiplier = 1.0
              End Select 
          End Function

 ----------

          Private Enum bonusLevel
               [No Bonus] = 0
               [Standard Bonus] = 5
               [Special Bonus] = 10
          End Enum

          Dim yourBonus as bonusLevel      ' Declares variable
          YourBonus = [Special Bonus]         ' Initializes variable