By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Enum.Parse doesn't trigger an exception (C# 2.0)

P: n/a
Hi,

I have the following code:

public enum MyColors { Red, Green, Blue }
MyColors c = (MyColors)Enum.Parse(typeof(MyColors), "abcd");

Why do I get c = "abcd" after this code instead of getting a
ArgumentException ?
MSDN says that this expetion is triggered when value is a name, but not
one of the named constants defined for the enumeration.
Thank you

Herve

Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
oops, my example is not correct. When the value is "abcd" it triggers
the right exception.
The problem is when the value is an integer string like "1234". In this
case, no exception is triggered and my enum now equals 1234.
Thank you

Herve

Nov 16 '05 #2

P: n/a
Hi,

When you pass the value (2nd) argument to Enum.Parse it will see if it can
match or convert the value that is passed. If the value is a empty string,
only has white space, or is a string but not one of the named constants
defined in the enumeration it will throw an ArgumentException. If it can
convert the value to the Enum’s underlying type it will create a new instance
of the enumeration with that value. For instance:

//with this enum
public enum Color { Red, Green, Blue }

//creates a new instance of Color with the value of 1 (Green)
Color c = (Color)Enum.Parse(typeof(Color), "1");
Console.WriteLine(c.ToString("G"));

//create a new instance of Color with the value of 3 (no name)
Color d = (Color)Enum.Parse(typeof(Color), "3");
Console.WriteLine(d.ToString("G"));

//create a new instance of Color with the value of 2 (Blue)
Color e = (Color)Enum.Parse(typeof(Color), "Blue");
Console.WriteLine(e.ToString("G"));

//Throw an ArgumentException because there is no match for the value
//and it cannot convert the value to the underlying type
Color f = (Color)Enum.Parse(typeof(Color), "Yellow");
Console.WriteLine(f.ToString("G"));
From the documentation I believe that this method has not changed from 1.1
to 2.0. If anyone knows any different please reply to this post.

I hope this helps
-----------------------
Nov 16 '05 #3

P: n/a
Your example is correct but you don't try to set a string integer like
"1234". As I said, it doesn't trigger an exception and I get my enum
variable with the value 1234.
Is it considered normal behaviour ?

Herve

Nov 16 '05 #4

P: n/a
Herve,

I would say yes this is normal behavior. In my example I used 3 but you can
substitute 1234 or any string that will convert to the underlying type of the
enum and the result will be the same. As long as it can be converted it will
not throw an exception.

Hope this helps.
--------------------
Nov 16 '05 #5

P: n/a
So what can I do to ensure that the value assigned is in the Range of
the enum ? I must test that on an enum I receive in parameter in a
method but I don't know what Type it is exactly (i.e. I know this is an
enum but I don't know this is a MyColors).

Thank you

Herve

Nov 16 '05 #6

P: n/a
Herve,

You can use either Enum.IsDefined(typeof(<enum>), value) which will return a
true or a false if the value falls within the range of the enum (best) or you
can use Enum.GetUnderlyingType(typeof(<enum>)) and see if the value that was
passed can be converted to that type.

I am not sure that I understand what you mean by you do not know what the
enum type is when it is passed to a method. If neither one of these methods
help can you post some code that I may look at? Maybe it will help me
understand your question better.

Hope this helps.
--------------------------
Nov 16 '05 #7

P: n/a
Thanks a lot Brian.
IsDefined works perfectly for my need.

May I ask something out of this topic ? While investigating for this
problem, I tried to output strings in the Visual C# 2005 beta console
window but nothing appears. I use for example:

System.Diagnostics.Debug.WriteLine("test");
Thank you

Herve

Nov 16 '05 #8

P: n/a
Herve,

Great! I am glad that it worked for you. As for your question about the
console window I would suggest that you go to the vs 2005 newsgroups and do a
search. I am sure that you will find the answer to that there.

Good Luck.
----------------------

--VS 2005 news groups
http://communities.microsoft.com/new...idbey&slcid=us

Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.