猿问

避免 if-else 梯子中的重复条件

我有四个复选框。下面一个是我的代码。我的问题是如何避免检查状态组合的代码重复。


法典:


if (chk1.Checked)

{

   if (chk2.Checked)

   {

      if (chk3.Checked)

      {

         if (chk4.Checked)

         {


         }

         else

         {


         }

      }

      else

      {

         if (chk4.Checked)

         {


         }

         else

         {


         }

      }

   }

}

与其他部分做同样的事情。


编辑:如果条件为真,我只想在字符串列表中添加一个项目。


侃侃无极
浏览 155回答 2
2回答

杨__羊羊

一种选择是预先构建操作字典。例如var states = new[]                {                     new { Checkboxes= new []{chk1,chk2,chk3,chk4},Action = new Action(()=>{ /* Do something */ })},                    new { Checkboxes= new []{chk1,chk2,chk3},Action = new Action(()=>{ /* Do something */ })},                    new { Checkboxes= new []{chk1,chk2,chk4},Action = new Action(()=>{ /* Do something */})},                // and so on                };states.First(x=>x.Checkboxes.All(c=>c.Checked)).Action();

沧海一幻觉

如果您只是想知道如何访问复选框的每个组合,则可以执行正在执行的操作,也可以合并ifif (chk1.Checked && chk2.Checked && chk3.Checked && chk4.Checked)// do somethingelse if (chk1.Checked && chk2.Checked && chk3.Checked && !chk4.Checked)&nbsp;// do somethingelse if (chk1.Checked && chk2.Checked && !chk3.Checked && chk4.Checked)&nbsp;...或者用Bits,Binary Literals(C#7)和a来获得一些花哨的东西,你可以做一些事情switchint BoolsToInt(params bool[] values)&nbsp;&nbsp; &nbsp; &nbsp;=> values.Aggregate(0, (current, value) => current << (value ? 1 : 0));...var val = BoolsToInt(chk1.Checked, chk2.Checked, chk3.Checked, chk4.Checked);switch (val){&nbsp; &nbsp;case 0b0000: break;&nbsp; &nbsp;case 0b0001: break;&nbsp; &nbsp;case 0b0010: break;&nbsp; &nbsp;case 0b0011: break;&nbsp; &nbsp;case 0b0100: break;&nbsp; &nbsp;case 0b0101: break;&nbsp; &nbsp;case 0b0110: break;&nbsp; &nbsp;case 0b0111: break;&nbsp; &nbsp;case 0b1000: break;&nbsp; &nbsp;case 0b1001: break;&nbsp; &nbsp;case 0b1010: break;&nbsp; &nbsp;case 0b1011: break;&nbsp; &nbsp;case 0b1100: break;&nbsp; &nbsp;case 0b1101: break;&nbsp; &nbsp;case 0b1110: break;&nbsp; &nbsp;case 0b1111: break;}注意:我不太确定最后一个选项是否更干净,而且更多是因为我只想使用二进制文字。 ifs可能是最好的选择或受到其他答案的启发private static Dictionary<int, Action> _dict;..._dict = new Dictionary<int, Action>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0000, () => DoStuff0() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0001, () => DoStuff1() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0010, () => DoStuff2() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0011, () => DoStuff3() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0100, () => DoStuff4() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0101, () => DoStuff5() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0110, () => DoStuff6() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b0111, () => DoStuff7() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1000, () => DoStuff8() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1001, () => DoStuff9() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1010, () => DoStuff10() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1011, () => DoStuff11() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1100, () => DoStuff12() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1101, () => DoStuff13() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1110, () => DoStuff14() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0b1111, () => DoStuff15() },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};用法var combination = BoolsToInt(chk1.Checked, chk2.Checked, chk3.Checked, chk4.Checked);dict[combination].Invoke();
随时随地看视频慕课网APP
我要回答