Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

.NET Enumerated Types Explained

0.00/5 (No votes)
3 Jul 2010 1  
Overview of enumerated types in .NET

Introduction

The purpose of this article is to provide an introduction to .NET enumerated types.

Background

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
        _PerformAddition()
    Case 2 ‘ Subtraction
        _PerformSubtraction()
    Case 3 ‘ Multiplication
        _PerformMultiplication()
End Select

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

Readability

From Wikipedia:

"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
End Type

Dim Operation As CalculatorOperations
Operation = _ GetOperation ()
Select Case Operation
    Case CalculatorOperations.Addition
        _PerformAddition()
    Case CalculatorOperations.Subtraction
       _PerformSubtraction()
    Case CalculatorOperations.Multiplication
       _PerformMultiplication()
End Select

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.

Bad Example

Dim ApplicationState as Integer = 5 ‘lets say five stands for Fatal Crash!
Sub _SetApplicationState(ByVal State As Integer)

Good Example

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 09
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
    _Authenticateuser()
End If
If LoginResult = False Then
     _InitiateLogin()
End If

As you can see, true and false allow for two conditions:

Dim LoginResult As AuthResult 
LoginResult = _AttemptLogin()
If LoginResult = AuthResult.Authenticated Then
    _Authenticateuser()
End If

If LoginResult = AuthResult.Failed Then
    _InitiateLogin()
End If

If LoginResult = AuthResult.Suspended Then
    _AlertSuspended()
End If

If LoginResult = AuthResult.AuthenticatedChangePassword Then
    _InitiatePasswordChange()
    _AuthenticateUser()
End If

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
End Type

Dim RGBVal As RGBValue
For RGBVal = RGBValue.RGB_FirstValue To RGBValue.RGB_LastValue
 ‘process here
Next

Conclusion

Well, I hope I’ve illustrated some of the benefits of using enumerated types. All of your feedback is welcome.

Thanks,
Bryan James
MCP, MCAD, MCSD.NET
http://www.bytepushers.com
http://www.twitter.com/budbjames

History

Version 1 07/3/2010

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here