473,403 Members | 2,071 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,403 software developers and data experts.

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 6319
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.