Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Please see below ASP.NET 4.5 code
C#
 if ( !Enum.TryParse( request.QueryString["q"], out cp.QuietZones ) )
cp.QuietZones = QuietZoneModules.Two;


I want corresponding 3.5 code. Enum.TryParse() is not in 3.5. Please help me.

thanks in advance,
Manu V Nath
Posted

TryParse has the following signature:

C#
ryParse<tenum>(string value, bool ignoreCase, out TEnum result)
    where TEnum : struct, new()
</tenum>


It has a generic type parameter
C#
TEnum 
that must be a
C#
struct 
and that it uses to determine the type of
C#
enum 
that it is parsing. When you don't provide it explicitly (as you did), it will take the type of the resultInputType variable, which is Enum (and not the type of the enumeration itself). Note that
C#
Enum 
is a class (even tough it inherits from ValueType) and therefore it does not satisfy the requirement that
C#
TEnum 
is a
C#
struct
.

You can solve this by giving the method a generic argument instead of a Type parameter. The generic type parameter
C#
TEnum 
must have the same constraints (i.e. struct) as the same parameter on the TryParse function. Otherwise you could put a class in the method while
C#
TryParse 
does not accept that.

So try this:

C#
private static TEnum getEnumStringEnumType<tenum>()
    where TEnum : struct
{
    string userInputString = string.Empty;
    TEnum resultInputType = default(TEnum);
    bool enumParseResult = false;

    while (!enumParseResult)
    {                
        userInputString = System.Console.ReadLine();
        enumParseResult = Enum.TryParse(userInputString, true, out resultInputType);
    }
    return resultInputType;
}
</tenum>


To call the method, use:

C#
getEnumStringEnumType<myenum>();</myenum>
 
Share this answer
 
v2
Hello,

As seen on StackOverFlow :
C#
public static bool EnumTryParse<T>(string strType,out T result)
{
    string strTypeFixed = strType.Replace(' ', '_');
    if (Enum.IsDefined(typeof(T), strTypeFixed))
    {
        result = (T)Enum.Parse(typeof(T), strTypeFixed, true);
        return true;
    }
    else
    {
        foreach (string value in Enum.GetNames(typeof(T)))
        {
            if (value.Equals(strTypeFixed, StringComparison.OrdinalIgnoreCase))
            {
                result = (T)Enum.Parse(typeof(T), value);
                return true;
            }
        }
        result = default(T);
        return false;
    }
}
 
Share this answer
 
 
Share this answer
 
Try a switch, something like this:
switch(cp.QuietZone)
{
    case "one":
      zone = QuietZoneModules.One;
      break;
    case "two":
      zone = QuietZoneModules.Two;
      break;
    case "three":
      zone = QuietZoneModules.Three;
      break;
    default:
      zone = QuietZoneModules.Two;
}


Or use a constant dictionary to look it up, using something like this:
Dictionary<string,> zone = new Dictionary<string,> = {
      {"one",QuietZoneModules.One}, {"def",QuietZoneModules.Two}, {"ghi",QuietZoneModules.Three}
    };


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900