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?

5
  • 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. Commented Mar 5, 2013 at 20:49
  • sorry should of mentioned the enum is a part of a another public class see above. Commented Mar 5, 2013 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. Commented Mar 5, 2013 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. Commented Mar 5, 2013 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? Commented Mar 5, 2013 at 21:11

3 Answers 3

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.

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

1 Comment

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
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.

Comments

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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.