【问题标题】:Check for string in several Enums with a switch statement使用 switch 语句检查多个枚举中的字符串
【发布时间】:2017-02-05 13:34:12
【问题描述】:

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

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 等枚举。

【问题讨论】:

  • switch(chosenName) { case NamesA.Adam.ToString (): //logic break; ....}
  • NamesA.Adam.ToString () 不是对 case 语句有效的常量表达式。
  • 请检查我的解决方案。我希望它能满足您的要求。
  • 哦,是的..我的错。错过了。

标签: c# enums switch-statement


【解决方案1】:

您需要利用整数,它是任何枚举的基础数据类型。看看我是如何做到的:

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

【讨论】:

  • 这是一个不错的解决方案。谢谢!
猜你喜欢
  • 2015-06-16
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多