Enumeration is a great way to assign easy to understand & remember textual names to your values. Consider the example:
Public Sub CityWork(ByVal CityID As Integer)
If CityID = 0 Then
'do something
ElseIf CityID = 1 Then
'do something
ElseIf CityID = 2 Then
'do something
Else
'do something
End If
End Sub
By the look of that, the code is pretty hard to understand as you have to remember what all those numbers are to associate with their function. To clarify things a bit, we can modify the above code using enumeration:
Public Enum CityTypes
SanDiego
SanFrancisco
SanJose
End Enum
Public Sub CityWork(ByVal CityID As CityTypes)
If CityID = CityTypes.SanDiego Then
'do something
ElseIf CityID = CityTypes.SanFrancisco Then
'do something
ElseIf CityID = CityTypes.SanJose Then
'do something
Else
'do something
End If
End Sub
Now this code is pretty much self-documented. It makes it so much easier to understand and will also allows you to take advantage of intellisense to help you code faster.
By default, the first enumerated value (SanDiego in this case) will have a value of 0. You can change that by explicitly assign an integer to it. The next enumerated value, if not specified, will be increased by 1 from the previous value (SanJose = 6)
Public Enum CityTypes
SanDiego = 1
SanFrancisco = 5
SanJose
End Enum
If you write your code this way, you'll find that it's much easier to understand, you can code faster, it's less prone to errors and at the same time, you pretty much self-documented the code.
Subscribe to:
Post Comments (Atom)
1 comment:
Good words.
Post a Comment