Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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.


Comments

JanVanRyswyck said:

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.

# June 14, 2007 6:37 AM

Michal Grzegorzewski said:

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

# June 14, 2007 8:11 AM

JanVanRyswyck said:

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.

# June 14, 2007 11:40 AM

sdbytnar said:

Derik,

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

# June 14, 2007 2:30 PM

Derik Whittaker said:

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

# June 15, 2007 8:03 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News