Enum.Parse()

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

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,"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 !

This is bewildering.
 
John A Grandy said:
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),...);
 
SearchRequest.SafeSearch =
(SafeSearchOptions)Enum.Parse(SafeSearchOptions,"moderate",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");
 
D.S. Fallow said:
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);
}
 
Back
Top