.NET Tip: Converting Strings to Enum Values Washington DC

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

Local Companies

Evergreen Computer Services, Inc
(301) 758-2986
12421 Alamanco Way
Washington, DC
Grytek
800-516-0840
11505 Cherry Tree Crossing RD
Cheltenham, MD
NGEN, LLC
(301) 531-9700
1101 Mercantile Lane
Washington, DC
Sophisticated Technologies, Inc.
(301) 731-1015
3311 Grayvine Lane
Washington, DC
L-Soft International, Inc.
(301) 731-0440
8100 Corporate Dr. Suite 350
Washington, DC
Total Service Solutions
(301) 306-7206
4601 Forbes Blvd.
Washington, DC
The Carrington Group, Inc
(202) 726-4441
1818 New York Ave., NE Suite 115
Washington, DC
CGH Technologies, Inc.
(202) 580-7400
600 Maryland Ave., SW
Washington, DC
Enlightened, Inc.
(202) 783-4655
666 11th St., NW
Washington, DC
recover data
001-9800000000
Co-Lane
City, NY

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

Evergreen Computer Services, Inc

(301) 758-2986
12421 Alamanco Way
Washington, DC

Related Local Events
DC Chamber Technology Series: Session 4
Dates: 12/10/2009 - 12/10/2009
Location: Robert H. Smith School at the Ronald Reagan Building and International Trade Center
Washington, DC
View Details

National Facilities Management & Technology (NFMT)
Dates: 3/16/2010 - 3/28/2010
Location: Baltimore Convention Center
Baltimore, MD
View Details

CSI 2009: The Next Phase In Security
Dates: 10/24/2009 - 10/30/2009
Location: Gaylord National Resort and Convention Center
National Harbor, MD
View Details

ACIs 3rd Annual Carbon Capture and Sequestration Summit
Dates: 9/14/2009 - 9/15/2009
Location: Omni Shoreham Hotel
Washington, DC
View Details

3rd Carbon Capture and Sequestration Summit
Dates: 9/14/2009 - 9/15/2009
Location: Omni Shoreham Hotel
Washington, DC
View Details