3

I have an enum which I use to find a coordinating string value. One of these enums has a space in it which I'm thus trying to use the description attribute to find that value. I'm having trouble casting back to the public class after it finds the DescriptionAttribute.

public class Address
{
   ...blah...more class datatypes here...

    public AddressType Type { get; set; }

    ...blah....

}

public enum AddressType
{
    FRA = 0,
    JAP = 1,
    MEX = 2,
    CAN = 3,
    [Description("United States")]
    UnitedStates = 4, 

}


 if (Address.Type.ToString() == "UnitedStates")
            {
               Adddress.Type = GetDescription(Address.Type);
            }

private static AddressType GetDescription(AddressType addrType)
    {
        FieldInfo fi = addrType.GetType().GetField(addrType.ToString());
        DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fi.GetCustomAttributes(
        typeof(DescriptionAttribute), false);
        return (attributes.Length > 0) ? attributes[0].Description : addrType.ToString();            
    }

Within the GetDescription method how do I cast it back to its public class data type 'AddressType' it fails because here its a string?

Jt2ouan
  • 1,964
  • 9
  • 33
  • 55
  • The enum is `AddrType` but your passing your method a parameter of type `AddressType`. You're also attempting to return a `string`, when the return type of your method is `AddressType`. – p.s.w.g Mar 05 '13 at 20:49
  • sorry should of mentioned the enum is a part of a another public class see above. – Jt2ouan Mar 05 '13 at 20:53
  • You're still going to have issues (at least given the code you've posted) because the type names do not match (`AddressType` vs `AddrType`) and the return type of the method you provided is not `string`. – p.s.w.g Mar 05 '13 at 20:56
  • thats what I'm having trouble with is the string. I need to cast it back to AddressType, addrType is just the local name I believe. – Jt2ouan Mar 05 '13 at 20:59
  • So you want to pass an `AddressType` into the method to get the `string` description, and then convert it back to the original `AddressType`? Why? – p.s.w.g Mar 05 '13 at 21:11

3 Answers3

3

I'm afraid I not 100% sure what you're asking for, but the following method returns the string description or name of provided AddressType.

private static string GetDescription(AddressType addrType)
{
    FieldInfo fi = addrType.GetType().GetField(addrType.ToString());
    DescriptionAttribute[] attributes = 
        (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
    return (attributes.Length > 0) ? attributes[0].Description : addrType.ToString();            
}

Notice the return type string.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • yes and where I'm having trouble is I'm looking to return it as AddressTpye because it is a part of my class which is an enum – Jt2ouan Mar 05 '13 at 21:07
0

You wont be able to directly cast a string to an enum. You will need to write a converter method that takes a string and returns the enum.

Simple Example but you could use a dictionary and make it its own class.

//string values are case sensitive
    private AddressType StringToEnum(string enumString)
            {
                AddressType returnValue;
                switch (enumString)
                {
                    case "United States":
                        returnValue = AddressType.UnitedStates;
                        break;
                    case "France":
                        returnValue = AddressType.FRA;
                        break;
                    case "Japan":
                        returnValue = AddressType.JAP;
                        break;
                    case "Mexico":
                        returnValue = AddressType.MEX;
                        break;
                    case "Canada":
                        returnValue = AddressType.CAN;
                        break;
                    default:
                        returnValue = AddressType.UnitedStates;
                        break;

                }
                return returnValue;
            }

if you are looking to convert a string to an enum you will need to do something like this.

scott lafoy
  • 1,001
  • 1
  • 13
  • 30
0

You could just use a helper method to remove the spaces from the sting and find the correct Enum

Example:

public T EnumFromString<T>(string value) where T : struct
{
    string noSpace = value.Replace(" ", "");
    if (Enum.GetNames(typeof(T)).Any(x => x.ToString().Equals(noSpace)))
    {
        return (T)Enum.Parse(typeof(T), noSpace);
    }
    return default(T);
}

Usage:

    public enum Test
    {
        UnitedStates,
        NewZealand
    }

    Test MyEnum = EnumFromString<Test>("New Zealand"); // Returns 'NewZealand'
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110