473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How do you convert a string into an enum?

Answer:
http://weblogs.asp.net/tims/archive/...02/106310.aspx
by Tim Sneath

I've come across the situation on a number of occasions when coding
where I've wanted to convert from a string to an enum. In the Media
Catalog sample, I resorted to one giant switch statement that has a
case block for each string that returns an enum from it.

One of my colleagues came up with the answer yesterday; it's one of
those methods that you can never find when you're looking for it, but
once discovered it seems blindingly obvious:

object Enum.Parse(System.Type enumType, string value, bool
ignoreCase);
So you can write the following kind of code:

enum Colour
{
Red,
Green,
Blue
}

// ...
Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);
Console.WriteLine("Colour Value: {0}", c.ToString());

// Picking an invalid colour throws an ArgumentException. To
// avoid this, call Enum.IsDefined() first, as follows:
string nonColour = "Polkadot";

if (Enum.IsDefined(typeof(Colour), nonColour))
c = (Colour) Enum.Parse(typeof(Colour), nonColour, true);
else
MessageBox.Show("Uh oh!");
What a time saver - thanks, Simon!

Footnote: interestingly, whilst writing this up I noticed that
Enum.IsDefined() doesn't offer the ignoreCase parameter. If you don't
know whether the casing is right, it seems the only way to do the
conversion is using the Parse method and catching the
ArgumentException. That's not ideal, since it runs a lot slower. I
wonder if this is a loophole in the design; no doubt someone like Brad
could cast light on it...

posted on Friday, April 02, 2004 1:49 PM
Nov 16 '05 #1
1 2286
use Enum.Parse. This will allow you to easily convert from string to
enum. To go in the other direction use MyEnumeration.ToString().

Allen Anderson
http://www.glacialcomponents.com
mailto: allen@put my website url here.com
On 5 Apr 2004 08:45:50 -0700, an******@hotmail.com (Anonieko Ramos)
wrote:
Answer:
http://weblogs.asp.net/tims/archive/...02/106310.aspx
by Tim Sneath

I've come across the situation on a number of occasions when coding
where I've wanted to convert from a string to an enum. In the Media
Catalog sample, I resorted to one giant switch statement that has a
case block for each string that returns an enum from it.

One of my colleagues came up with the answer yesterday; it's one of
those methods that you can never find when you're looking for it, but
once discovered it seems blindingly obvious:

object Enum.Parse(System.Type enumType, string value, bool
ignoreCase);
So you can write the following kind of code:

enum Colour
{
Red,
Green,
Blue
}

// ...
Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);
Console.WriteLine("Colour Value: {0}", c.ToString());

// Picking an invalid colour throws an ArgumentException. To
// avoid this, call Enum.IsDefined() first, as follows:
string nonColour = "Polkadot";

if (Enum.IsDefined(typeof(Colour), nonColour))
c = (Colour) Enum.Parse(typeof(Colour), nonColour, true);
else
MessageBox.Show("Uh oh!");
What a time saver - thanks, Simon!

Footnote: interestingly, whilst writing this up I noticed that
Enum.IsDefined() doesn't offer the ignoreCase parameter. If you don't
know whether the casing is right, it seems the only way to do the
conversion is using the Parse method and catching the
ArgumentException. That's not ideal, since it runs a lot slower. I
wonder if this is a loophole in the design; no doubt someone like Brad
could cast light on it...

posted on Friday, April 02, 2004 1:49 PM


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Billingsley | last post by:
Is there any significant difference between int variable1 = (int) variable2; and int variable1 = Convert.ToInt32(variable2); It seems like the extra typing and granularity of .ToInt32 vs....
1
by: Anonieko Ramos | last post by:
Answer: http://weblogs.asp.net/tims/archive/2004/04/02/106310.aspx by Tim Sneath I've come across the situation on a number of occasions when coding where I've wanted to convert from a string...
3
by: Shimon Sim | last post by:
I am writing general method that would take Type that should represent a enum that do something with it. In the middle of the method I need to cast it to this enum. I tried Convert.ChangeType...
0
by: Toska | last post by:
Hi, Wonder how I can convert a string value to enum value. Code ------------------- <appSettings> <add key="BaseType" value="Sand" /> </appSettings>
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
3
by: Tariq Ahmad | last post by:
got this code from codeproject.com, but cant convert it to vb. can u help please? thnx. t. ---------------------------------------------------------------------------- ------- Append a...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
3
by: QuocSi | last post by:
Hi all, I have a bitsfield Structure MyBits a as boolean b as boolean c as boolean d as boolean end structure
3
by: rsennat | last post by:
Hi, Converting enum to strings is simple. Just we can use the enum as the i ndex to get the string from an array of strings. But how do I convert string to enum. Should I need to compare with...
0
by: Bum | last post by:
Hello all, I have a C dll I need to tap into and some C code to interpret to vb.net 2005, but am so far unsuccessful. I just get the generic "runtime has encountered a fatal error at XXXXXX...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.