473,471 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
8 6322
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
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...
6
by: Brian Haynes | last post by:
I've read all the posts in this forum that I can find that look related to this issue and I have only found 1 solution that I consider to be a bit of a hack. What I want to do is assign a value to...
3
by: slamb | last post by:
Hi all, Does anyone know of a way to deserialize xml data that has an element that represents an enum value but is actually an int? I know I can use to tag enum values, that works but is too...
5
by: genc_ ymeri at hotmail dot com | last post by:
well, I making up a "scenario" which may not be the best but I can make my point at least techincally..,. I have this enum : public enum JustTest { Volvo = 0x32324234, Acura = 0x32423443,...
1
by: John A Grandy | last post by:
Primitives like Int32 provide a Parse() method , and TryParse() method -- which is very useful. Enum provides a Parse() method, but not a TryParse() method. Other than wrapping the...
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.