C# cast string to enum with enum attribute

Posted by rubentjeuh on Stack Overflow See other posts from Stack Overflow or by rubentjeuh
Published on 2010-05-07T09:28:26Z Indexed on 2010/05/07 9:38 UTC
Read the original article Hit count: 153

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about convert