1

Enum.TryParse(,,out) not supporting in vs2008 in c#? why? I am trying to use but getting error that TryParse no defined.

9
  • Shame, poor you! It takes 2-3 lines of code to write. Stop complaining. Commented Aug 19, 2010 at 9:49
  • @leppie..! I did not know about this method so that I am just asking the question. If this is shameful for me and if you know the code what I have to write then please send me the code line. I am new in dotnet. Commented Aug 19, 2010 at 9:54
  • 1
    see I am not mentioned anywhere that I am reading about method.... I wanted quick answer so that I came here please stop arguing...!and I wont expect that MS should do this. Commented Aug 19, 2010 at 10:03
  • 1
    @leppie: When using the classic view on MSDN library, it erroneously tells you that Enum.TryParse -- and many other new types, methods etc -- are available in 3.5. Commented Aug 19, 2010 at 10:13
  • 1
    @Lalit: Sorry, I saw the post where you got confused. Given you an upvote now, and downvoted the source of your issue ;P Commented Aug 19, 2010 at 10:14

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2
 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

According to another question of hes I think he wants to parse a enum name (of string) into a enum not a number.
2

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

Thanks for guidance sir. If I would want to use this functionality (not method) in 3.5 then what i have to do ?
@Lalit: Take the answer from Michael ;)
@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 , but how to use it then , if it supports in 3.5
@Lalit: It doesn't support 3.5 - the classic view documentation is wrong. (Apologies if my original comment was misleading.)
1

This question includes a number of implementation approaches: How to TryParse for Enum value?

1 Comment

Thanks this should useful for me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.