The purpose of this article is to provide an introduction to .NET enumerated types.
Code readability is a big factor when considering the quality of source code. The easier code is to understand, the easier it is to maintain. Have you ever found yourself using numbers to represent a range of variable values? For example:
Dim CalculationOperation As Integer = 0
CalculationOperation = _ GetOperation ()
Select Case CalculationOperation
Case 1 ’ Addition
Case 2 ‘ Subtraction
Case 3 ‘ Multiplication
This requires you as well as any other developers that might touch your code to remember all of the possible numeric values that represents colors. This can be a maintenance nightmare! To solve this problem, VB.NET has enumerated types.
Reasons to Use Enumerated Types
"In computer programming, an enumerated type (also called enumeration or
enum) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language."
Enumerated types allow you to give an English description to a range of integer values. Perhaps an example will explain this.
Public Type CalculatorOperations
Addition = 1
Subtraction = 2
Multiplication = 3
Dim Operation As CalculatorOperations
Operation = _ GetOperation ()
Select Case Operation
This routine is easier to read.
When coding a routine, be sure to consider numeric literals that represent a value other than the number itself as a possible reason to create an enumerated type.
Enums as Routine Parameters
Enumerated types are great as routine parameters. Consider the following example.
Dim ApplicationState as Integer = 5 ‘lets say five stands for Fatal Crash!
Sub _SetApplicationState(ByVal State As Integer)
Dim ApplicationState As AppState = AppState.FatalCrash
Sub _SetApplicationState(ByVal State As AppState)
If you are using Visual Studio, then you no doubt have noticed the benefit of using enumerated types as function parameters. While you are writing the code to call the routine, intellisense will show you all available members of the enumerated type.
Compiler Type Checking
Using enumerated types also provides type checking by the compiler. Consider the following block of code:
‘Valid color values are 0 – 9
Dim CurrentColor As Integer
CurrentColor = 12 ‘invalid color
This is impossible with enumerated types. Enumerated types can make powerful return values. Consider the following code:
Dim LoginResult As Boolean = false
LoginResult = _AttemptLogin()
If LoginResult = True Then
If LoginResult = False Then
As you can see,
false allow for two conditions:
Dim LoginResult As AuthResult
LoginResult = _AttemptLogin()
If LoginResult = AuthResult.Authenticated Then
If LoginResult = AuthResult.Failed Then
If LoginResult = AuthResult.Suspended Then
If LoginResult = AuthResult.AuthenticatedChangePassword Then
Do you see the possibilities?
Define the First and Last Entry as Loop Limits
You may find yourself in a situation where you need to iterate through each member of your enumerated type. One suggested practice is to reserve the first and last element as loop limits.
Public Type RGBValue
RGB_FirstValue = 0
RGB_Red = 0
RGB_Green = 1
RGB_Blue = 2
RGB_LastValue = 2
Dim RGBVal As RGBValue
For RGBVal = RGBValue.RGB_FirstValue To RGBValue.RGB_LastValue
Well, I hope I’ve illustrated some of the benefits of using enumerated types. All of your feedback is welcome.
MCP, MCAD, MCSD.NET
Version 1 07/3/2010
I'm a 28 year old software engineer from Tennessee. I've been programming since I was 13 years old. I'm fortunate enough to be able to do what I love for a living.
I'm MCP, MCAD, MCSD.NET certified.
follow me on twitter