ksdev
ksdev

Reputation: 56

Avoid repeat conditions in if-else ladder

I have four checkboxes. Below one is my code. My question is how to avoid code duplication for check state combination.

Code :

if (chk1.Checked)
{
   if (chk2.Checked)
   {
      if (chk3.Checked)
      {
         if (chk4.Checked)
         {

         }
         else
         {

         }
      }
      else
      {
         if (chk4.Checked)
         {

         }
         else
         {

         }
      }
   }
}

Same thing to do with else part.

Edit: I just want to add an item in the list of a string if condition is true.

Upvotes: 0

Views: 111

Answers (2)

TheGeneral
TheGeneral

Reputation: 81513

If you are just wondering how to access every combination of checkboxes, you could do what you are doing, or you could also coalesce the ifs

if (chk1.Checked && chk2.Checked && chk3.Checked && chk4.Checked)
// do something
else if (chk1.Checked && chk2.Checked && chk3.Checked && !chk4.Checked) 
// do something
else if (chk1.Checked && chk2.Checked && !chk3.Checked && chk4.Checked) 
...

or to get a little fancy with Bits, Binary Literals (C#7) and a switch you could do something this

int BoolsToInt(params bool[] values) 
     => values.Aggregate(0, (current, value) => current << (value ? 1 : 0));

...

var val = BoolsToInt(chk1.Checked, chk2.Checked, chk3.Checked, chk4.Checked);
switch (val)
{
   case 0b0000: break;
   case 0b0001: break;
   case 0b0010: break;
   case 0b0011: break;
   case 0b0100: break;
   case 0b0101: break;
   case 0b0110: break;
   case 0b0111: break;
   case 0b1000: break;
   case 0b1001: break;
   case 0b1010: break;
   case 0b1011: break;
   case 0b1100: break;
   case 0b1101: break;
   case 0b1110: break;
   case 0b1111: break;
}

Note : i'm not really sure the last option is any cleaner and was more because i just wanted to use binary literals. the ifs are probably the best option

or inspired by the other answer

private static Dictionary<int, Action> _dict;

...

_dict = new Dictionary<int, Action>()
               {
                  { 0b0000, () => DoStuff0() },
                  { 0b0001, () => DoStuff1() },
                  { 0b0010, () => DoStuff2() },
                  { 0b0011, () => DoStuff3() },
                  { 0b0100, () => DoStuff4() },
                  { 0b0101, () => DoStuff5() },
                  { 0b0110, () => DoStuff6() },
                  { 0b0111, () => DoStuff7() },
                  { 0b1000, () => DoStuff8() },
                  { 0b1001, () => DoStuff9() },
                  { 0b1010, () => DoStuff10() },
                  { 0b1011, () => DoStuff11() },
                  { 0b1100, () => DoStuff12() },
                  { 0b1101, () => DoStuff13() },
                  { 0b1110, () => DoStuff14() },
                  { 0b1111, () => DoStuff15() },
               };

Usage

var combination = BoolsToInt(chk1.Checked, chk2.Checked, chk3.Checked, chk4.Checked);
dict[combination].Invoke();

Upvotes: 0

Anu Viswan
Anu Viswan

Reputation: 18153

One option is to build a dictionary of Actions upfront.

For example,

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

Upvotes: 1

Related Questions