473,804 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum.Parse()

Is this the hardest method in the entire .NET class lib?

Seems like it works differently for different enumTypes

I code

SearchRequest.S afeSearch =
(SafeSearchOpti ons)Enum.Parse( SafeSearchOptio ns,"moderate",t rue);

and it tells me that "SafeSearchOpti ons is a type but is used like a
variable".

But the Enum.Parse() docs say that the 1st param should be an enumType !

This is bewildering.
Apr 5 '06 #1
3 4030
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote:
SearchRequest. SafeSearch =
(SafeSearchOpt ions)Enum.Parse (SafeSearchOpti ons,"moderate", true);
and it tells me that "SafeSearchOpti ons is a type but is used like a
variable".
But the Enum.Parse() docs say that the 1st param should be an enumType !


The docs mean it should be a System.Type, one that represents an
enumeration. So presumably
Enum.Parse(type of(SafeSearchOp tions),...);

--
Lucian
Apr 5 '06 #2
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in news:
#R************* @TK2MSFTNGP05.p hx.gbl:
SearchRequest.S afeSearch =
(SafeSearchOpti ons)Enum.Parse( SafeSearchOptio ns,"moderate",t rue);

and it tells me that "SafeSearchOpti ons is a type but is used like a
variable".


MyEnum.Member var = (MyEnum) Enum.Parse(type of(MyEnum), "whatever") ;

--
Klaus H. Probst, MVP
http://www.simulplex.net/
Apr 6 '06 #3
SP

"D.S. Fallow" <ms****@msnews. com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in news:
#R************* @TK2MSFTNGP05.p hx.gbl:
SearchRequest.S afeSearch =
(SafeSearchOpti ons)Enum.Parse( SafeSearchOptio ns,"moderate",t rue);

and it tells me that "SafeSearchOpti ons is a type but is used like a
variable".


MyEnum.Member var = (MyEnum) Enum.Parse(type of(MyEnum), "whatever") ;


If you are using .Net 2.0 then you can use the following method. Put it in a
utility class.

public static T ParseEnum<T>(st ring s)
{
return (T)Enum.Parse(t ypeof(T), s);
}

Apr 7 '06 #4

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

Similar topics

21
4610
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
375
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
8
6344
by: cadilhac | last post by:
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
2
1461
by: wapsiii | last post by:
I'm trying to do this http://geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx public enum Color { RED, GREEN, BLUE }
2
3402
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function and return the name "FirstData"
8
8618
by: kc | last post by:
I'm trying to pull data from a database that I have no control over the structure of. There's a members table with a column for the member's sex. It just stores the sex as M or F. I'd like to create an enum to store the sex. Is there a way that I can create a method in the Enum to convert M to Male and F to Female, or does this method have to exist in another class? Thanks for any help!
34
11210
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?
3
5238
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
5
7650
by: kjqua | last post by:
Hi all, I am newbie in c# and i have a question. I try to work with serial port. and i will like to configure the serial port by a form I add in a ComboBox.parity some items like: None Odd
10
6138
by: Doug | last post by:
I have an enum that will look kind of like this: private enum MyEnum { SomeValue = 0, AnotherValue = 1, ThirdValue = 2 }
0
9714
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, 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...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, 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...
0
10346
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 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...
1
10347
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
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 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...
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.