Rusi Nova
Rusi Nova

Reputation: 2655

Generic Method to get string from ENUM (by passing int Value)

i have written a generic method to convert int to Enum String, getting an error. Please Help Thanx.

    public static string GetStringEquiValentOFEnumFromString<T>(int enumVal)
        where T : struct
    {
        if(Enum.IsDefined(typeof(T),enumVal))
        {
            return ((T)enumVal).ToString();  ///Error: Cannot convert type 'int' to 'T'
        }          
        return null;  
    }

Upvotes: 0

Views: 2087

Answers (4)

Rohit
Rohit

Reputation: 1

public static class StringEnum {

    public static string GetStringValue(Enum value)
    {
        string output = null;
        Type type = value.GetType();

        FieldInfo fi = type.GetField(value.ToString());
        StringValue[] attr = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];

        if (attr.Length > 0)
        {
            output = attr[0].Value;
        }
        return output;
    }
}

StringEnum is a class which have (GetStringValue) method to get the string value.

public enum CampaignRequestType { [StringValue("None")] None = 0, [StringValue("Pharmacy Cards")] Pharmacy_Cards = 1,[StringValue("Prospect Campaign")] Prospect_Campaign = 2,[StringValue("Tradeshow/Advertising")] Tradeshow_Advertising = 3 }

its a enum...

string item = StringEnum.GetStringValue((Enumeration.CampaignRequestType)updateRequestStatus.RequestType_Code);

here (Enumeration.CampaignRequestType) is my enumeration and updateRequestStatus.RequestType_Code is data base field int type

i cast int value to enumeration type

Upvotes: 0

Ron Warholic
Ron Warholic

Reputation: 10074

You're seeing this error because generic type arguments cannot be constrained to the enum type in C#. Since your constraint only says it has to be a value type the compiler cannot guarantee that enumVal will be convertible to T and gives a compile time error.

To get around this you can explicitly define the enum type in the function:

public static string GetStringFromValue(this MyEnum enum, int val) {
   if (Enum.IsDefined(typeof(MyEnum), val)) {
      return ((MyEnum) val).ToString();
   }
   return null;
}

Or you can use something like Jon Skeet's UnconstrainedMelody library which rewrites your code at the bytecode level to enforce the enum constraint (callable from another assembly).

Upvotes: 0

phoog
phoog

Reputation: 43046

This ought to work:

public static string GetStringEquiValentOFEnumFromString<T>(int enumVal) 
    where T : struct 
{ 
    if(Enum.IsDefined(typeof(T),enumVal)) 
    { 
        return ((T)(object)enumVal).ToString();
    }           
    return null;   
} 

Upvotes: 0

DMac the Destroyer
DMac the Destroyer

Reputation: 5290

I think you can use Enum.ToObject:

public static string GetStringEquiValentOFEnumFromString<T>(int enumVal)
     where T : struct
    {
        if (Enum.IsDefined(typeof(T), enumVal))
        {
            return Enum.ToObject(typeof (T), enumVal).ToString();
        }
    }

http://msdn.microsoft.com/en-us/library/system.enum.toobject.aspx

Upvotes: 2

Related Questions