tags:

views:

77

answers:

4

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
Thanks for guidance sir.
Lalit
+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
Thanks for guidance sir. If I would want to use this functionality (not method) in 3.5 then what i have to do ?
Lalit
@Lalit: Take the answer from Michael ;)
Oliver
@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
@LukeH , but how to use it then , if it supports in 3.5
Lalit
@Lalit: It *doesn't* support 3.5 - the classic view documentation is wrong. (Apologies if my original comment was misleading.)
LukeH
ha ha ha ...!ok ok...getting....
Lalit
+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
According to another question of hes I think he wants to parse a enum name (of string) into a enum not a number.
lasseespeholt
@lasseespeholtyes you are right,
Lalit
+1  A: 

This question includes a number of implementation approaches: http://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value

Richard
Thanks this should useful for me!
Lalit