Hi,
I have the following code:
public enum MyColors { Red, Green, Blue }
MyColors c = (MyColors)Enum. Parse(typeof(My Colors), "abcd");
Why do I get c = "abcd" after this code instead of getting a
ArgumentExcepti on ?
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 8 6344
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
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 ArgumentExcepti on. 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.Par se(typeof(Color ), "1");
Console.WriteLi ne(c.ToString(" G"));
//create a new instance of Color with the value of 3 (no name)
Color d = (Color)Enum.Par se(typeof(Color ), "3");
Console.WriteLi ne(d.ToString(" G"));
//create a new instance of Color with the value of 2 (Blue)
Color e = (Color)Enum.Par se(typeof(Color ), "Blue");
Console.WriteLi ne(e.ToString(" G"));
//Throw an ArgumentExcepti on because there is no match for the value
//and it cannot convert the value to the underlying type
Color f = (Color)Enum.Par se(typeof(Color ), "Yellow");
Console.WriteLi ne(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
-----------------------
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
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.
--------------------
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
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.GetUnderly ingType(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.
--------------------------
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.Diagnost ics.Debug.Write Line("test");
Thank you
Herve
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
{
|
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 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
|
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 an enum variable using an int.
What I am using right now is something like this:
public enum MyEnum
{
value1,
value2,
value3
|
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 messy. I have many enums and tagging all values would not
be good.
xml data
|
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,
Benz = 0x23423434
| |
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 Enum.Parse() in an exception handler , can someone
recommend a technique similar to TryParse() ?
Thanks.
|
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 detail, the enum is declared as,
namespace test{
enum MyEnum{
VALUE1,VALUE2
};
|
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 snippet that I wrote today that gives me
an instant implementation of the pattern. I could easily just always
use such an implementation instead of a standard enum, so I wanted to
know what you experts all thought.
Is there a case for standard enums?
|
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 we follow the path below:
intValue --enum name --enum value
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |