Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / Visual Basic
Article

Tips about .NET Enums

Rate me:
Please Sign up or sign in to vote.
4.26/5 (32 votes)
19 Apr 20041 min read 277.1K   48   32
Some very simple tips using Enum types in VB.NET and C#.NET

Introduction

The following lines of code show you how to retrieve an Enum member, given its integer value or its name. This can be very useful when you are using Enum types in VB.NET or C#.NET and you need to set an Enum type value loading, for example, a string configuration setting from a .config file.

In the following, we suppose to have these .NET namespaces referenced and this custom Enum type defined:

VB
' VB.NET version
Imports System.ComponentModel
Imports System.Diagnostics

Public Enum MyEnum
  alfa = 1
  beta = 2
  gamma = 3
End Enum
C#
// C#.NET version
using System.ComponentModel;
using System.Diagnostics;

public enum MyEnum
{
  alfa = 1,
  beta = 2,
  gamma = 3
}

How to retrieve an Enum member given its integer value

To retrieve an Enum member given its integer value, simply cast the integer value to your Enum type:

VB
' VB.NET version
Dim MyEnumVal As MyEnum
Dim i As Integer

i = 2
MyEnumVal = CType(i, MyEnum)
Debug.WriteLine(MyEnumVal.ToString())

The result of the Debug.WriteLine() method will be "beta".

Notice that if you try to cast an integer value that is not defined in the Enum, the code will work without exceptions; in the following code, MyEnumVal receives the value of 4, anyway:

VB
' VB.NET version
i = 4
MyEnumVal = CType(i, MyEnum)
Debug.WriteLine(MyEnumVal.ToString())    ' The output is "4"

As suggested by Michael Kennedy (thank you!), with some computing overhead, you can test for defined Enum values using this code:

VB
' VB.NET version
If Not MyEnum.IsDefined(GetType(MyEnum), 4) Then
  Debug.WriteLine("The value of 4 is not defined in the Enum")
End If

See below for the C#.NET version:

C#
// C#.NET version
MyEnum MyEnumVal;
int i;

// Retrieve an enum member by its value
i = 2;
MyEnumVal = (MyEnum)i;
Debug.WriteLine(MyEnumVal.ToString());

// If the specified value is not an enum member,
// MyEnumVal receives the value anyway
i = 4;
MyEnumVal = (MyEnum)i;
Debug.WriteLine(MyEnumVal.ToString());    // The output is "4"

// Test for allowed enum values
if (!MyEnum.IsDefined(typeof(MyEnum), 4))
  Debug.WriteLine("The value of 4 is not defined in the Enum");

How to retrieve an Enum member given its name

To retrieve an Enum member given its name, use the ConvertFrom() method of the TypeConverter class and cast the result to your Enum type:

VB
' VB.NET version
MyEnumVal = CType(TypeDescriptor.GetConverter(MyEnumVal).ConvertFrom("gamma"), MyEnum)
Debug.WriteLine(MyEnumVal.ToString())    ' The output is "gamma"

An alternative way to reach the same goal is using the Parse() method of System.Enum. This approach, suggested by Dactyl (thank you!), is simpler and 3-times faster than using the TypeConverter class:

VB
' VB.NET version
MyEnumVal = CType(System.Enum.Parse(GetType(MyEnum), "gamma"), MyEnum)

See below for the C#.NET version:

C#
// C#.NET version
MyEnumVal = (MyEnum) TypeDescriptor.GetConverter(MyEnumVal).ConvertFrom("gamma");
Debug.WriteLine(MyEnumVal.ToString());    // The output is "gamma"

// Dactyl's alternative
MyEnumVal = (MyEnum) Enum.Parse(typeof(MyEnum), "gamma");

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


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
GeneralAnother trick: - Fill the .Items collection of a drop down list with the Names and Values of an Enum Pin
Pedro Francisco Borges Pereira19-Jul-07 6:33
Pedro Francisco Borges Pereira19-Jul-07 6:33 
General'How to retrieve an Enum member given its name' Pin
Pedro Francisco Borges Pereira12-Jul-07 3:24
Pedro Francisco Borges Pereira12-Jul-07 3:24 
GeneralRe: 'How to retrieve an Enum member given its name' Pin
Christian Graus12-Jul-07 3:32
protectorChristian Graus12-Jul-07 3:32 
GeneralRe: 'How to retrieve an Enum member given its name' Pin
Pedro Francisco Borges Pereira12-Jul-07 4:56
Pedro Francisco Borges Pereira12-Jul-07 4:56 
GeneralRe: 'How to retrieve an Enum member given its name' Pin
Alberto Venditti12-Jul-07 3:33
Alberto Venditti12-Jul-07 3:33 
GeneralRe: 'How to retrieve an Enum member given its name' Pin
Pedro Francisco Borges Pereira12-Jul-07 4:48
Pedro Francisco Borges Pereira12-Jul-07 4:48 
Yes, of course.
In that context, I can see the utility.

Thanks!
GeneralIsDefined - Not Reliable Pin
wakazula15-Mar-07 4:34
wakazula15-Mar-07 4:34 
GeneralRe: IsDefined - Not Reliable Pin
Alberto Venditti5-Mar-15 0:11
Alberto Venditti5-Mar-15 0:11 
QuestionHow to pass argument in image button's click handler Pin
Tarun Dudhatra7-Mar-07 1:23
Tarun Dudhatra7-Mar-07 1:23 
AnswerRe: How to pass argument in image button's click handler Pin
Alberto Venditti7-Mar-07 6:55
Alberto Venditti7-Mar-07 6:55 
GeneralUsing a key word as an enum element Pin
David Parvin17-May-06 11:51
David Parvin17-May-06 11:51 
GeneralThe Best Point of the Article Pin
Ricardo Casquete17-Feb-06 23:40
Ricardo Casquete17-Feb-06 23:40 
GeneralDirectCast Pin
MadClown1-Dec-05 11:29
MadClown1-Dec-05 11:29 
GeneralRe: DirectCast Pin
Alberto Venditti1-Dec-05 22:34
Alberto Venditti1-Dec-05 22:34 
QuestionHow to declare an Enum item with a Space Pin
Anonymous3-Aug-05 9:23
Anonymous3-Aug-05 9:23 
AnswerRe: How to declare an Enum item with a Space Pin
Alberto Venditti3-Aug-05 22:12
Alberto Venditti3-Aug-05 22:12 
AnswerRe: How to declare an Enum item with a Space Pin
BradleyT15-Aug-05 12:12
BradleyT15-Aug-05 12:12 
AnswerRe: How to declare an Enum item with a Space Pin
Artur M.21-Nov-05 7:00
Artur M.21-Nov-05 7:00 
GeneralSeveral Enums in one integer Pin
Member 124044920-Jul-04 2:21
Member 124044920-Jul-04 2:21 
GeneralRe: Several Enums in one integer Pin
Anonymous21-Jul-04 2:38
Anonymous21-Jul-04 2:38 
GeneralAbout Enums From Microsoft Pin
Michael Kennedy18-May-04 10:50
Michael Kennedy18-May-04 10:50 
GeneralAnother trick to put in your article Pin
Arjan Einbu29-Apr-04 1:24
Arjan Einbu29-Apr-04 1:24 
GeneralNulls become zero Pin
A Berglas27-Apr-04 17:57
A Berglas27-Apr-04 17:57 
GeneralGetting all values of an enum Pin
mav.northwind22-Apr-04 21:06
mav.northwind22-Apr-04 21:06 
GeneralRe: Getting all values of an enum Pin
28-Apr-04 1:26
suss28-Apr-04 1:26 

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.