Enum.TryParse(,,out) not supporting in vs2008 in c#? why? I am trying to use but getting error that TryParse no defined.
4 Answers
Enum.TryParse was introduced in .NET 4. However, you might like to use my Unconstrained Melody library which has something similar, and many other features.
Comments
public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
{
returnValue = default(T);
int intEnumValue;
if (Int32.TryParse(valueToParse, out intEnumValue))
{
if (Enum.IsDefined(typeof(T), intEnumValue))
{
returnValue = (T)(object)intEnumValue;
return true;
}
}
return false;
}
1 Comment
Lasse Espeholt
According to another question of hes I think he wants to parse a enum name (of string) into a enum not a number.
As per MSDN, Enum.TryParse was not added until .NET 4. VS2008 targets up to .NET 3.5SP1, so that is why you cannot access this method.
5 Comments
Red Swan
Thanks for guidance sir. If I would want to use this functionality (not method) in 3.5 then what i have to do ?
Oliver
@Lalit: Take the answer from Michael ;)
LukeH
@Kent: If, like me, you use the classic view on the MSDN docs then it will erroneously tell you that
Enum.TryParse is available in 3.5.Red Swan
@LukeH , but how to use it then , if it supports in 3.5
LukeH
@Lalit: It doesn't support 3.5 - the classic view documentation is wrong. (Apologies if my original comment was misleading.)
This question includes a number of implementation approaches: How to TryParse for Enum value?
1 Comment
Red Swan
Thanks this should useful for me!
Enum.TryParse-- and many other new types, methods etc -- are available in 3.5.