Click here to Skip to main content
13,406,671 members (43,061 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 1 Jul 2010

.NET Enumerated Types Explained

, 3 Jul 2010
Rate this:
Please Sign up or sign in to vote.
Overview of enumerated types in .NET


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


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
    Case CalculatorOperations.Subtraction
    Case CalculatorOperations.Multiplication
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
End If
If LoginResult = False Then
End If

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

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

If LoginResult = AuthResult.Failed Then
End If

If LoginResult = AuthResult.Suspended Then
End If

If LoginResult = AuthResult.AuthenticatedChangePassword Then
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


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

Bryan James


Version 1 07/3/2010


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
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.

VB.NET, ASP.NET, C#, assembly, HTML, JavaScript, AJAX

I'm MCP, MCAD, MCSD.NET certified.

follow me on twitter

You may also be interested in...


Comments and Discussions

GeneralNice explaination Pin
Bigdeak17-Aug-10 4:31
memberBigdeak17-Aug-10 4:31 
GeneralRe: Nice explaination Pin
buddy.james23-Sep-10 22:29
memberbuddy.james23-Sep-10 22:29 
GeneralPretty good explanation for newbies Pin
Shane Story7-Jul-10 10:10
memberShane Story7-Jul-10 10:10 
GeneralRe: Pretty good explanation for newbies Pin
bjames027-Jul-10 19:33
memberbjames027-Jul-10 19:33 
GeneralNot an article Pin
John Simmons / outlaw programmer3-Jul-10 3:01
memberJohn Simmons / outlaw programmer3-Jul-10 3:01 
GeneralNeeds formatting Pin
Richard MacCutchan3-Jul-10 0:04
mvpRichard MacCutchan3-Jul-10 0:04 
GeneralRe: Needs formatting Pin
bjames023-Jul-10 1:09
memberbjames023-Jul-10 1:09 
GeneralRe: Needs formatting Pin
Richard MacCutchan3-Jul-10 1:37
mvpRichard MacCutchan3-Jul-10 1:37 
GeneralRe: Needs formatting Pin
John Simmons / outlaw programmer3-Jul-10 3:00
memberJohn Simmons / outlaw programmer3-Jul-10 3:00 
I reformatted it for you. Your formatting problems stem from using some sort of html editor when you wrote your article. Generally, this is a BAD idea because it puts a lot of useless crap in your text that isn't necessary here at CP. This site has it's own styles, and adding html tags should pretty much be limited to the following:

<H1> through <H4>

There is absolutely no reason to add styles or classes to your tags here. By using an editor that "converts" or "exports" to html, you introduce a lot of useless crap. It took me almost 30 minutes to clean it up.
.45 ACP - because shooting twice is just silly
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

GeneralRe: Needs formatting Pin
bjames025-Jul-10 18:42
memberbjames025-Jul-10 18:42 
GeneralRe: Needs formatting Pin
SSDiver21126-Jul-10 19:15
memberSSDiver21126-Jul-10 19:15 
GeneralRe: Needs formatting Pin
John Simmons / outlaw programmer7-Jul-10 12:31
memberJohn Simmons / outlaw programmer7-Jul-10 12:31 
GeneralRe: Needs formatting Pin
bjames027-Jul-10 19:29
memberbjames027-Jul-10 19:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 3 Jul 2010
Article Copyright 2010 by bjames02
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid