Red Swan
Red Swan

Reputation: 15545

Enum.TryParse not supporting in vs2008 in c#

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

Upvotes: 1

Views: 1101

Answers (4)

Kent Boogaart
Kent Boogaart

Reputation: 178630

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.

Upvotes: 2

Richard
Richard

Reputation: 108975

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

Upvotes: 1

Michael Pakhantsov
Michael Pakhantsov

Reputation: 25370

 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;
  }

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499870

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.

Upvotes: 6

Related Questions