views:

670

answers:

4

Hello, i've got the following question: I've got

public enum Als 
    {
        [StringValue("Beantwoord")] Beantwoord = 0,
        [StringValue("Niet beantwoord")] NietBeantwoord = 1,
        [StringValue("Geselecteerd")] Geselecteerd = 2,
        [StringValue("Niet geselecteerd")] NietGeselecteerd = 3,
    }

with

public class StringValueAttribute : System.Attribute
    {

        private string _value;

        public StringValueAttribute(string value)
        {
            _value = value;
        }

        public string Value
        {
        get { return _value; }
        }

    }

And i would like to put the value from the item I selected of a combobox into a int:

int i = ((int)(Als)Enum.Parse(typeof(Als), (string)cboAls.SelectedValue)); //<- WRONG

Is this possible, and if so, how? (the stringvalue matches the value selected from the combobox)

Thanks

+4  A: 

Here's a helper method that should point you in the write direction.

protected Als GetEnumByStringValueAttribute(string value)
{
    Type enumType = typeof(Als);
    foreach (Enum val in Enum.GetValues(enumType))
    {
        FieldInfo fi = enumType.GetField(val.ToString());
        StringValueAttribute[] attributes = (StringValueAttribute[])fi.GetCustomAttributes(
            typeof(StringValueAttribute), false);
        StringValueAttribute attr = attributes[0];
        if (attr.Value == value)
        {
            return (Als)val;
        }
    }
    throw new ArgumentException("The value '" + value + "' is not supported.");
}

And to call it, just do the following:

Als result = this.GetEnumByStringValueAttribute<Als>(ComboBox.SelectedValue);

This probably isn't the best solution though as it's tied to Als and you'll probably want to make this code re-usable. What you'll probably want to strip out the code from my solution to return you the attribute value and then just use Enum.Parse as you are doing in your question.

GenericTypeTea
A: 

I'm using the DescriptionAttribute from Microsoft and the following extension method:

public static string GetDescription(this Enum value)
{
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }

    string description = value.ToString();
    FieldInfo fieldInfo = value.GetType().GetField(description);
    DescriptionAttribute[] attributes =
       (DescriptionAttribute[])
     fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        description = attributes[0].Description;
    }
    return description;
}
Oliver
A: 

Not sure if I am missing something here, can you not do this,

Als temp = (Als)combo1.SelectedItem;
int t = (int)temp;
theraneman
A: 

You propably use the StringValue to display and want to get back from that to the enum. In that case, this should work:

int t =
    (from value in Enum.GetValues(typeof(Als))
    where GetDescription(value) == (string)cbo.SelectedValue
    select (int)value).First();

public static string GetDescription(Enum value)
{
    FieldInfo field = value.GetType().GetField(value.ToString());
    var attribute =
        (StringValueAttribute[])field.GetCustomerAttributes(
        typeof(StringValueAttribute), false);
    return (attributes.Length > 0) ? attributes[0].Value : value.ToString();
}

btw. You'd be better of using the DescriptionAttribute.

Steven