It has the following advantages:
- It eliminates the hard coding of literal values.
- When used the code is more readable and easy to maintain.
- The constants within the enumeration receive values automatically unless specified by the user
- Naming the constants within enumeration is easy and they are distinguishable by the containing enumerator even when the same name is defined in other enumerator.
The simple example of Enumeration is illustrated as below.
The customer is filling out restaurant survey. Upon completion of the survey total score is calculated and prompts the restaurant to take some action based on it. The responses to the questionnaire are categorized as Excellent, Good, Average, Fair, and Poor. Define the enumerator as:
Enum EnumResponse
Excellent = 2
Good = 1
Average = 0
Fair = -1
Poor = -2
End Enum
Enum EnumResult
Excellent = 10
Good = 5
Average = 0
Fair = -5
Poor = -10
End Enum
The UI contains radiobuttonlist control to get an answer to each question. The following function will return the score.
Function GetScore(ByVal ctlRadioBtn As Web.UI.WebControls.RadioButtonList) As Int16
Select Case ctlRadioBtn.Selected.Value
Case EnumResponse.Excellent
Score = 2
Case EnumResponse.Good
Score = 1
Case EnumResponse.Average
Score = 0
Case EnumResponse.Fair
Score = -1
Case EnumResponse.Poor
Score = -2
End Select
End Function
One more common problem with the programmers is comparison of strings. The problem is in determination of the case of the string, whether it is lower case or upper case or mixed case. The program does not work for the first time then debugging kicks in and correction is made in the form of hard coding the literal value to appropriate case. Why not use enumeration here?
More explanation with the restaurant survey example is as below. The question is “Would you dine here again?”. The UI presents three options Yes, No and Maybe using DropDownList box control and these options are coded in HTML.
One may write the code to compare the selection as
If ctlDropDownBox.Selected.Text = "Yes" Then
'…
ElseIf ctlDropDownBox.Selected.Text = "No" Then
'….
ElseIf ctlDropDownBox.Selected.Text = "Maybe" Then
'…
End If
Imagine the above dropdownlist box is now getting filled from database with values as YES, NO, MAYBE. The above code will surely fail to do its intended job. Instead if you would have written the code using enumeration as below then no revisit to code is required or visit to enumerator definition or change to its contents are more efficient than finding and replacing literal strings.
Enum EnumPrompts
YES
NO
MAYBE
End Enum
selectedEntry = UCase(ctlDropDownBox.Selected.Text)
If selectedEntry = GetName(EnumPrompts.YES) Then
'…
ElseIf selectedEntry = GetName(EnumPrompts.NO) Then
'….
ElseIf selectedEntry = GetName(EnumPrompts.MAYBE) Then
'…
End If
Final Note: As you have seen the enumerations avoid common mistakes made by the programmer, use it wisely where needed. Though it may not 100 percent eliminate literal strings but 95 percent will be cleaned out for certain.