Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Checking a value against an Enum (Enum.IsDefined)

Here is a simple snippet of code that I always forget and have to look up time and time again. 

This will check to see if a value (string or int) is a match for a enum.  This can be useful when trying to load data from a database which is in key the form and you would like to represent that value as an enum. 

In order to do this your enum has to have an implecit values defined for the enum.


The code below will not allow you to have consistant results and should not be used for this code.

 

Here is the code snippet to check for the enum as well as to cast it into the actual enum value.



As you can see, the code for this is pertty straight forward, but for some reason I always forget how to do it and have to relearn it every time.
Posted 06-14-2007 4:40 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

JanVanRyswyck wrote re: Checking a value against an Enum (Enum.IsDefined)
on 06-14-2007 6:37 AM

This is indeed something that .NET developers often forget. I must admit that I am guilty as well. However, when I remember to check whether a value is defined for a particular enum , I tend to not use the Enum.IsDefined static method. This method uses reflection and must load metadata, thus performance is less than optimal. What I usually do is a simple comparison with the lowest and highest  value defined for this particular enum, e.g.

if(value < WeekDay.Sunday || value > WeekDay.Saturday)

{

throw new InvalidEnumArgumentException( ... );

}

The only disadvantage of this approach is that I have to change my validation code as soon as we get a week with 8 days.

Michal Grzegorzewski wrote re: Checking a value against an Enum (Enum.IsDefined)
on 06-14-2007 8:11 AM

JanVanRyswyck: 8-day week? It doesn't happen too often ;)

But the approach is far better than using Enum.IsDefined, the only thing should be considered is testing. After adding new value to the enum at least one test should fail.

btw: Cwalina, 5.7.2

http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756

JanVanRyswyck wrote re: Checking a value against an Enum (Enum.IsDefined)
on 06-14-2007 11:40 AM

mgrzeg: regarding the 8-day week: I forgot the smiley ;-)

I usually have two unit tests for covering the if statement. Indeed, when the enum changes, I get at least one failure.

sdbytnar wrote re: Checking a value against an Enum (Enum.IsDefined)
on 06-14-2007 2:30 PM

Derik,

According to Microsoft guidelines, you shouldn't explicitly declare an enum as an int, it's the default. :)

Derik Whittaker wrote re: Checking a value against an Enum (Enum.IsDefined)
on 06-15-2007 8:03 PM

@Sdbytnar,

You are right, it is the default.  But i like to do it in order for it to be explicit.  I tend to favor readability over speed., assuming that the speed difference is not TOO large.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)