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

Enum.Parse()

P: n/a
Is this the hardest method in the entire .NET class lib?

Seems like it works differently for different enumTypes

I code

SearchRequest.SafeSearch =
(SafeSearchOptions)Enum.Parse(SafeSearchOptions,"m oderate",true);

and it tells me that "SafeSearchOptions 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
Share this Question
Share on Google+
3 Replies


P: n/a
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote:
SearchRequest.SafeSearch =
(SafeSearchOptions)Enum.Parse(SafeSearchOptions," moderate",true);
and it tells me that "SafeSearchOptions 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(typeof(SafeSearchOptions),...);

--
Lucian
Apr 5 '06 #2

P: n/a
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in news:
#R*************@TK2MSFTNGP05.phx.gbl:
SearchRequest.SafeSearch =
(SafeSearchOptions)Enum.Parse(SafeSearchOptions,"m oderate",true);

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


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

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

P: n/a
SP

"D.S. Fallow" <ms****@msnews.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in news:
#R*************@TK2MSFTNGP05.phx.gbl:
SearchRequest.SafeSearch =
(SafeSearchOptions)Enum.Parse(SafeSearchOptions,"m oderate",true);

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


MyEnum.Member var = (MyEnum) Enum.Parse(typeof(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>(string s)
{
return (T)Enum.Parse(typeof(T), s);
}

Apr 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.