locked
How can I get a value from an enum RRS feed

  • Question

  • I knew how to do this but cannot get it work now.


    Lets suppose you have the following enum and somebody as hardcoded the values against each one of
    them.

    public enum Cars
    {
         Ferrari=0,
         Lamborghini=1,
         Spider=2,
         AstonMartin=3
    }

    I know i can do this to get the name from an enum

    //The result will be "Spider"
    Cars
    mycar=GetEnum<Cars>("2");

    public T GetEnum<T>(string enumValue)
    {
        return (T)Enum.Parse(typeof(T), enumValue);
    }


    Now lets suppose i want to extract a value from value from the enum (EG spider) how do you do it?

    //Get the value associated with spider the result  should be 2
    short carType=??



    How do you do it?
    thanks
    Thanks for your help
    Friday, July 4, 2008 5:05 AM

Answers

  • Here's how you get the value that is ascociated with a member of an enum type:

        public partial class Form1 : Form  
        {  
            enum Cars  
            {  
                Ferrari = 0,  
                Lamborghini = 1,  
                Spider = 2,  
                AstonMartin = 3  
            }  
     
            public Form1()  
            {  
                InitializeComponent();  
                comboBox1.Items.AddRange(Enum.GetNames(typeof(Cars)));  
                comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);  
            }  
     
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                ComboBox cbox = (ComboBox)sender;  
                string name = cbox.SelectedItem.ToString();  
                if (Enum.IsDefined(typeof(Cars), name))  
                {  
                    Cars car = (Cars)Enum.Parse(typeof(Cars), name);  
                    int value = (int)car;  
                    System.Diagnostics.Debug.WriteLine(value);  
                }  
            }  
        } 

    /Calle
    • Proposed as answer by Calle Mellergardh Friday, July 4, 2008 1:00 PM
    • Marked as answer by devBrix Friday, July 4, 2008 3:57 PM
    Friday, July 4, 2008 1:00 PM

All replies

  • I'm not sure if this is what you need, but this gives you your desired result.

    short carType=(short)Cars.Spider;
    or
    short carType=(short)myCar;
    Friday, July 4, 2008 6:25 AM
  •  Thanks for your reply but not exactly.

    Lets suppose somebody has filled in a combo from an enumeration.

    Now you select  AstonMartin from your combo.

    you want to want to get the value that AstonMartin as been associated to.

    I dont think you can do that way.  I know there is something like "Enum.GetValues" but no sure how you use it.

    any more suggestions
    Thanks for your help
    Friday, July 4, 2008 8:30 AM
  • Here's how you get the value that is ascociated with a member of an enum type:

        public partial class Form1 : Form  
        {  
            enum Cars  
            {  
                Ferrari = 0,  
                Lamborghini = 1,  
                Spider = 2,  
                AstonMartin = 3  
            }  
     
            public Form1()  
            {  
                InitializeComponent();  
                comboBox1.Items.AddRange(Enum.GetNames(typeof(Cars)));  
                comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);  
            }  
     
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                ComboBox cbox = (ComboBox)sender;  
                string name = cbox.SelectedItem.ToString();  
                if (Enum.IsDefined(typeof(Cars), name))  
                {  
                    Cars car = (Cars)Enum.Parse(typeof(Cars), name);  
                    int value = (int)car;  
                    System.Diagnostics.Debug.WriteLine(value);  
                }  
            }  
        } 

    /Calle
    • Proposed as answer by Calle Mellergardh Friday, July 4, 2008 1:00 PM
    • Marked as answer by devBrix Friday, July 4, 2008 3:57 PM
    Friday, July 4, 2008 1:00 PM
  •  Thanks a lot .
    That's is it for  me.Problem solved I understand.

    Thanks for your help
    Friday, July 4, 2008 3:56 PM