Enum.TryParse(,,out) not supporting in vs2008 in c#? why? I am trying to use but getting error that TryParse no defined.
+6
A:
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.
Jon Skeet
2010-08-19 09:51:47
Thanks for guidance sir.
Lalit
2010-08-19 09:56:33
+2
A:
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.
HTH,
Kent
Kent Boogaart
2010-08-19 09:53:04
Thanks for guidance sir. If I would want to use this functionality (not method) in 3.5 then what i have to do ?
Lalit
2010-08-19 09:57:21
@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.
LukeH
2010-08-19 10:22:33
@Lalit: It *doesn't* support 3.5 - the classic view documentation is wrong. (Apologies if my original comment was misleading.)
LukeH
2010-08-19 10:42:28
+2
A:
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;
}
Michael Pakhantsov
2010-08-19 09:56:11
According to another question of hes I think he wants to parse a enum name (of string) into a enum not a number.
lasseespeholt
2010-08-19 09:58:56
+1
A:
This question includes a number of implementation approaches: http://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value
Richard
2010-08-19 09:58:03