2017-02-05 1 views
1

두 열거 형 중 하나의 값 또는 둘 모두의 값으로 채울 수있는 목록 상자가 있습니다. 기본적으로이 코드 :switch 문을 사용하여 여러 Enum에서 문자열 검사

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

을 ...하지만 난 스위치를 사용하여 선호 :

은 내가 이런 식으로 확인할 수 있습니다 알고 있습니다. 작동 방법을 찾을 수 없습니다. 누구나 아이디어가 있으십니까?


편집 : 나는 또한, 미래 NamesC 처리 할 수있는 스위치의 여지가있을 가지고 ... D ... 등 E, 열거 말을해야한다. 솔루션의

+0

'스위치 (chosenName) {경우 NamesA.Adam.ToString 내가 그것을 달성 한 방법에서보세요() : // 논리 중단; ....}' – Developer

+0

'NamesA.Adam.ToString()'은 case 문에 유효한 상수 표현식이 아닙니다. – RBT

+0

내 해결책을 확인하십시오. 나는 그것이 당신의 요구 사항을 만족 시키기를 바랍니다. – RBT

답변

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; 
      } 
     } 
} 
} 
+1

좋은 해결책입니다. 감사! – MrAtoni

-1

일이 될 수 있습니다 : 당신은 어떤 열거의 기본 데이터 형식 인 정수를 활용할 필요가

public void Solution() 
    { 
     List<string> lst = new List<string>(); 
     lst.AddRange(Enum.GetNames(typeof(NamesA))); 
     string chosenName = "Bob"; 

     NamesA _result; 
     NamesB _resultB; 

     try 
     { 

      _result = (NamesA)Enum.Parse(typeof(NamesA), chosenName); 

      switch (_result) 
      { 
       case NamesA.Adam: 
        //{ /*some stuff happens*/ } 
        break; 
       case NamesA.Albert: 
        //{ /*some stuff happens*/ } 
        break; 
      } 
     } 
     catch (ArgumentException) 
     { 
      _resultB = (NamesB)Enum.Parse(typeof(NamesB), chosenName); 

      switch (_resultB) 
      { 
       case NamesB.Bert: 
        //{ /*some stuff happens*/ } 
        break; 
       case NamesB.Bob: 
        //{ /*some stuff happens*/ } 
        break; 
      } 
     } 
    } 
+0

선택한 열거 형이 NamesB이면 무엇입니까? – Developer

+0

제 질문을 조금 편집했습니다. 솔루션은 또한 미래의 NamesC, D, E ... enums를 처리 할 수 ​​있어야합니다. -/ – MrAtoni

+0

@MrAtoni 그래서 열거 형 멤버의 미래 멤버를위한 마법의 switch 문을 원하십니까? –

관련 문제