.NET Tip: Converting Strings to Enum Values Jacksonville FL

Learn how to take a string and convert it into an enumeration value.

Local Companies

M A C P R O Software Inc
904-296-3900
6650 Southpoint Pkwy
Jacksonville, FL
EZ Wealth Solution
904-554-7987
1720 Detroit St
Jacksonville, FL
Gobeille Automation
904-631-8267
2421 Glen Gardner Dr
Jacksonville, FL
Advanced Technologies Solutions Inc
904-363-2223
2950 Halcyon Ln
Jacksonville, FL
Applied Engineering Management Corp
904-260-0411
7400 Baymeadows Way
Jacksonville, FL
Orion Software Group Inc
904-998-0034
9770 Old Baymeadows RD
Jacksonville, FL
Sun IT Solution
904-731-7330
7899 Baymeadows Way
Jacksonville, FL
American Medical Systems Inc
904-636-6742
7400 Baymeadows Way
Jacksonville, FL
Amtech Computer Services Inc
904-247-7752
2315 Beach Blvd Jacksonville Beach
Jacksonville, FL
Computers & Components
(904) 262-3460
11728 Mina Rd
Jacksonville, FL

provided by: 
Originally published at Internet.com


Occasionally, I run across the need to see whether a string holds a valid enumeration value. I prefer to not add comparisons to specific string values when I already have an enumeration with all possible valid values. The solution is to try to convert the string into an enumeration value. If the string does not hold a valid enumeration value, an error will be thrown. If the string does hold a valid enumeration value, you will have a variable with the matching enumeration value that you can work with as needed. First, take a look at the enumeration I'll be using: public enum DaysOfWeek { Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7 }

It isn't the most interesting enumeration, but it will suffice. Below, I begin with a string holding the value I want to convert. Next, to do the conversion I use the Enum.Parse() method. The Parse() method takes the type of the enumeration you want to convert to as well as the string to convert. If the conversion succeeds, you now can use CurrentDay however you like in your application. If the string cannot be converted to a valid enumeration value, you can catch the error and process it appropriately. string CurrentDayString = "Sunday"; DaysOfWeek CurrentDay = DaysOfWeek.Monday; try { CurrentDay = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), CurrentDayString); } catch { // Invalid enumeration value } switch (CurrentDay) { case DaysOfWeek.Saturday: case DaysOfWeek.Sunday: // What are you doing working on the weekend break; default: // Get back to work break; }

The version of Enum.Parse() that I showed in this example is case sensitive. So, if my initial string had been "sunday" instead of "Sunday", it would have thrown an error. There is an overload of Enum.Parse() that accepts an additional boolean value that tells it to ignore case when doing the conversion. Your application will dictate which is most appropriate for you to use.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

M A C P R O Software Inc

904-296-3900
6650 Southpoint Pkwy
Jacksonville, FL
http://www.macproworld.com

Related Local Events
PWC EXCEL-erated Program
Dates: 12/8/2009 - 12/8/2009
Location: Hotel Indigo – Deerwood
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 1/26/2010 - 1/26/2010
Location: Florida State College at Jacksonville- Advanced Technology Center
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 11/24/2009 - 11/24/2009
Location: Florida State College at Jacksonville- Advanced Technology Center
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 10/27/2009 - 10/27/2009
Location: FCCJ Advanced Technology Center
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 10/27/2009 - 10/27/2009
Location: FCCJ Advanced Technology Center
Jacksonville, FL
View Details