Introduction to Enumerations
Introduction
Imagine you are creating a program for a real estate company (a business that sells houses). You want the program to ask a customer the type of house she wants to purchase and/or the type of garage that the desired house should have. Here is an example:
Module Exercise Public Function Main() As Integer Dim TypeOfHouse As Integer Dim TypeOfGarage As Integer TypeOfHouse = InputBox("Enter the type of house you want to purchase" & vbCrLf & "1 - Single Family" & vbCrLf & "2 - Townhouse" & vbCrLf & "3 - Condominium" & vbCrLf & "Your Choice: ") TypeOfGarage = InputBox("Enter the type of garage you want" & vbCrLf & "0 - Doesn't matter" & vbCrLf & "1 - Interior" & vbCrLf & "2 - Exterior" & vbCrLf & "Your Choice: ") MsgBox("House Type: " & TypeOfHouse & vbCrLf & "Garage Type: " & TypeOfGarage) Return 0 End Function End Module
Here is an example of running the program:
For such a program, the numbers can be vague. 1 can be considered a general number but, in our program, it can represent a Single Family house or an Interior type of garage. At the same time, our program uses the constant 1 in particular meaningful ways. To make it possible to give more meaning to a constant number, when the number can be made part of a series, you can/should create a type of list.
An enumeration is a series of constant integers that each has a specific position in the list and can be recognized by a meaningful name. Based on this, instead of just remembering that the constant 1 represents Single Family, you can create a list that has that type of house. In another list, instead of using 1 again, you can give it a name. Consequently, in each list, although the constant 1 would still be considered, at least it would mean something precise.
Creating an Enumeration |
Tou can create an enumeration manually or let Microsoft Visual Basic create a skeleton code for you. To manually write the code, on a first line of code, type the Enum keyword followed by a name. On another line, end the enumeration with End Enum. Here is an example:
Enum HouseTypes End Enum
If you are using Microsoft Visual Studio and you it to generate an enumeration code for you, right-click the section where you want to add it and click Insert Snippet... In the menu, double-click Code Patterns, If, For Each, Property, etc... Double-click Enum, Generics, Interfaces, Structures. Double-click Define an Enumeration:
The section between the Enum Name and the End Enum lines is the body of the enumeration. In that body, add the members of the enumeration, each on its own line. Here is an example:
Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum
A Variable of an Enumeration Type |
After creating an enumeration, it becomes a type and can be used like a data type. For example, you can declare a variable of its type. You use the same formula of any variable. Here is an example:
Module Exercise Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum Public Function Main() As Integer Dim ht As HouseType Return 0 End Function End Module
After declaring the variable, you can use it as you see fit. Here is an example:
Module Exercise
Enum HouseType
Unknown
SingleFamily
TownHouse
Condominium
End Enum
Public Function Main() As Integer
Dim ht As HouseType
MsgBox(ht)
Return 0
End Function
End Module
To initialize a variable of an enumeration type, you must specify which member of the enumeration would be assigned to the variable. You should only assign a known member of the enumeration. To do this, on the right side of the assignment operator, type the name of the enumeration, followed by the period operator, and followed by the member whose value you want to assign. Here is an example:
Module Exercise
Enum HouseType
Unknown
SingleFamily
TownHouse
Condominium
End Enum
Public Function Main() As Integer
Dim ht As HouseType
ht = HouseType.SingleFamily
MsgBox(ht)
Return 0
End Function
End Module
The Values of Members of an Enumeration |
Each member of an enumeration holds a value of a natural number, such as 0, 1, 2, 3, etc. The number 0 is given, to the first member. The number 1 is given to the second member, and so on. If you don't like those values, you can change them.
Suppose you want the first member to hold a value other than 0. To specify the value of a member, assign to the member using the assignment operator "=". Here is an example:
Enum HouseType Unknown = 5 SingleFamily TownHouse Condominium End Enum
After doing this, the member that follows it would receive a value + 1. In our example, Unknown now would have a value of 5, SingleFamily would have a value of 6 because it follows a member whose value is 1 (thus 5 + 1 = 6). Townhouse would have a value of 7, and Condominium would have a value of 8.
You can also assign a value to more than one member of an enumeration. To do this, simply assign the desired value to any member. Here are examples:
Enum HouseType Unknown = 5 SingleFamily = 14 TownHouse Condominium = 8 End Enum
In this case, Townhouse would have a value of 15 because it follows SingleFamily that has a value of 14.
Enumerations Visibility
You can control an enumeration's accessibility outside of its project. This means that you can hide or make it visible outside of its project. To do this, you can precede it with the Private, the Protected, the Friend, or the Public keyword. Here is an example:
Module Exercise
Public Enum HouseType
Unknown = 5
SingleFamily = 14
TownHouse
Condominium = 8
End Enum
Public Function Main() As Integer
Dim ht As HouseType
ht = HouseType.SingleFamily
Return 0
End Function
End Module
Passing an Enumeration as Argument |
As mentioned already, once an enumeration has been created, it becomes a type. It can be passed as argument and it can be returned from a function.
You pass an enumeration as argument using the same approach of a normal data type. Here is an example:
Private Sub ShowHouse(ByVal PropType As HouseType) End Sub
In the same way, you can pass as many enumeration types as necessary. In the body of the procedure, you can use the enumeration as you see fit. When calling the procedure, pass an argument that holds a value of the type of enumeration. Here is an example:
Module Exercise Public Enum HouseType Unknown = 2 SingleFamily = 4 TownHouse = 6 Condominium = 8 End Enum Private Sub ShowHouse(ByVal PropType As HouseType) MsgBox("Type of house: " & PropType) End Sub Public Function Main() As Integer Dim ht As HouseType ht = HouseType.SingleFamily ShowHouse(ht) Return 0 End Function End Module
This would produce:
You can also pass the argument as optional. When creating the procedure, use the Optional keyword to specify that the argument has a default value. When calling the procedure, you can pass or omit passing a value for the argument. Here is an example:
Module Exercise Public Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum Private Sub ShowHouse(Optional ByVal PropType As HouseType = HouseType.Unknown) MsgBox("Type of house: " & PropType) End Sub Public Function Main() As Integer Dim ht As HouseType ht = HouseType.SingleFamily ShowHouse() Return 0 End Function End Module
This would produce:
Returning an Enumeration From a Function |
To create a function that returns an enumeration, specify its As factor with the name of the enumeration. In the body of the enumeration, do whatever you have to do. Before exiting the function, make sure you return a value that is the type of the enumeration. Here is an example:
Private Function SpecifyPropertyType() As HouseType Dim pt As HouseType pt = HouseType.TownHouse Return pt End Function
You can call a function that returns an enumeration type by using just its name. Otherwise, you can use its returned value. Here is an example:
Module Exercise Public Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum Private Sub ShowHouse(Optional ByVal PropType As HouseType = HouseType.Unknown) MsgBox("Type of house: " & PropType) End Sub Private Function SpecifyPropertyType() As HouseType Dim pt As HouseType pt = HouseType.TownHouse Return pt End Function Public Function Main() As Integer Dim ht As HouseType ht = SpecifyPropertyType() ShowHouse(ht) Return 0 End Function End Module
This would produce: