MrAtoni MrAtoni - 4 years ago 90
C# Question

Check for string in several Enums with a switch statement

I have a listbox that could be filled with values from one of two enums, or both of them. Basically this code:

public enum NamesA { Adam , Albert }
public enum NamesB { Bert , Bob }


and later

List<string> nameList = new List<string>();

if (reason) nameList.AddRange(Enum.GetNames(typeof(NamesA)));
else if (reason) nameList.AddRange(Enum.GetNames(typeof(NamesB)));
else
{
nameList.AddRange(Enum.GetNames(typeof(NamesA)));
nameList.AddRange(Enum.GetNames(typeof(NamesB)));
}

foreach (string name in nameList)
listBox1.Items.Add(name);


My problem is that later in the code I want different things to happen depending on the users choice in the listbox.

I know I can check it like this:

string chosenName = listBox1.SelectedItem.ToString();

if (chosenName.Equals(NamesA.Adam.ToString()))
{ /*some stuff happens*/ }
else if (chosenName.Equals(NamesB.Bob.ToString()))
{ /*other stuff happens*/ }
/* ...and so on */


...but I would prefer using a Switch.
I can't find a way for it to work. Anyone have an idea?




EDIT: I should also say that there has to be room for the switch to handle future NamesC, ...D, ...E, etc. enums.

RBT RBT
Answer Source

You need to leverage the integer which is the underlying data type of any enum. Have a look at how I've achieved it:

File: CustomListItem.cs

namespace WindowsFormsApplication1
{
    public class CustomListItem
    {
        public string Text { get; set; }
        public int EnumValue { get; set; }

    }
}

File: Form1.cs

namespace WindowsFormsApplication1
{
    public enum NamesA { Adam = 0, Albert = 1 };
    public enum NamesB { Bert = 2, Bob = 3 }

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List <CustomListItem> nameList = new List<CustomListItem>();
            int reason = 1;
            if (reason == 1)
                foreach (NamesA item in Enum.GetValues(typeof(NamesA)))
                    nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
            else if (reason == 2)
            {
                foreach (NamesB item in Enum.GetValues(typeof(NamesB)))
                    nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
            }
            else
            {
                foreach (NamesA item in Enum.GetValues(typeof(NamesA)))
                    nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
                foreach (NamesB item in Enum.GetValues(typeof(NamesB)))
                nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
            }
            listBox1.DataSource = nameList;
            listBox1.DisplayMember = "Text";
            listBox1.ValueMember = "EnumValue";

        }

        private void button_Click(object sender, EventArgs e)
        {
            int chosenNameEnumValue = (int) listBox1.SelectedValue;

            switch (chosenNameEnumValue)
            {
                case (int) NamesA.Adam:
                    /*some stuff happens*/
                    break;
                case (int) NamesA.Albert:
                    /*other stuff happens*/
                    break;
                case (int)NamesB.Bert:
                    /*some stuff happens*/
                    break;
                case (int)NamesB.Bob:
                    /*other stuff happens*/
                    break;
                default:
                    /* ...and so on */
                    break;
            }
        }
}
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download