使用switch语句检查多个枚举中的字符串

时间:2017-02-05 13:34:13

标签: c# enums switch-statement

我有一个列表框,可以填充两个枚举中的一个或两个枚举的值。基本上这段代码:

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

以后

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);

我的问题是,稍后在代码中我希望根据列表框中的用户选择发生不同的事情。

我知道我可以这样检查:

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 */

...但我更喜欢使用Switch。 我无法找到工作方式。有人有想法吗?

编辑:我还应该说,交换机必须有空间来处理未来的NamesC,...... D,...... E等枚举。

1 个答案:

答案 0 :(得分:0)

您需要利用整数作为任何枚举的基础数据类型。看看我是如何实现它的:

文件:CustomListItem.cs

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

    }
}

文件: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;
            }
        }
}
}