Is there a more efficient way to do things according to which checkboxes are checked?

I only recently encountered this problem myself, and the search for this problem has not yet borne fruit ...

So, let's say I have four checkboxes as an example, and I want to do certain things, based on which they are checked at any time ...

if (CB1.Checked)
{
    //Do things here if only checkbox 1 is checked.
}
else if (CB2.Checked)
{
    //Do things here if only checkbox 2 is checked.
}
else if (CB3.Checked)
{
    //Do things here if only checkbox 3 is checked.
}
else //if (CB4.Checked)
{
    //Do things here if only checkbox 4 is checked.
}

I am sure that most people will use something like the code snippet of the example above or its variant. It looks simple, right? But what if ... you are not just checking only one flag?

if (CB1.Checked && CB2.Checked)
{
    //Do things here if only checkbox 1 & 2 is checked.
}
else if (CB2.Checked && CB3.Checked)
{
    //Do things here if only checkbox 2 & 3 is checked.
}
else if (CB3.Checked && CB1.Checked)
{
    //Do things here if only checkbox 3 & 1 is checked.
}
else if (CB4.Checked && CB1.Checked)
{
    //Do things here if only checkbox 4 & 1 is checked.
}
else if (CB4.Checked && CB2.Checked)
{
    //Do things here if only checkbox 4 & 2 is checked.
}
else //if (CB4.Checked && CB3.Checked)
{
    //Do things here if only checkbox 4 & 3 is checked.
}

As you can see ... the number of if-else expressions has increased ... And this will be, if you want to compare, perhaps even more flags than 4, or for more flags from 4 ... And this can complicate the situation, and most programmers cannot avoid this.

, , , :

private int GetNumberOfCheckboxesChecked()
{
    int NumberofCheckBoxesChecked = 0;
    foreach (Control c in groupBox1.Controls)
    {
        if ((c is CheckBox) && ((CheckBox)c).Checked)
            NumberofCheckBoxesChecked++;
    }

    return NumberofCheckBoxesChecked;
}

, checkbox checkchanged :

private void OneAtLeast(object originalSender)
{
    CheckBox tempCB = (CheckBox)originalSender;
    if (!CB1.Checked && !CB2.Checked && !CB3.Checked && !CB4.Checked)
    {
        tempCB.Checked = true;
        MessageBox.Show("You must select at least one option!", "Invalid Operation", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

, ... ( , ) , / ? ( )?

, , , , .

, switch-case , ... , , . , if- .

+4
2

, . , , if. , .

16 , 4 , switch , @Damien_The_Unbeliever. , switch lookup, - , "" . , , , , , , . ApplesCheckBox, SeedsCheckBox, SoilCheckBox, .

. , @Damien_The_Unbeliever :

[Flags]
enum CheckboxActions
{
    None = 0,

    CheckBox1 = 1,
    CheckBox2 = 2,
    CheckBox3 = 4,
    CheckBox4 = 8,

    DoJustCB1 = 1,
    DoJustCB2 = 2,
    DoCB1AndCB2ButNeverCB4 = 3
}

var option = CheckboxActions.None;

if(CB1.Checked) option = option | CheckboxActions.CheckBox1;
if(CB2.Checked) option = option | CheckboxActions.CheckBox2;
if(CB3.Checked) option = option | CheckboxActions.CheckBox3;
if(CB4.Checked) option = option | CheckboxActions.CheckBox4;

, , :

switch (option)
{
    case CheckboxActions.None:
        DoNothingNoOptionsSet();
        break;
    case CheckBoxActions.DoJustCB1:
        DoJustCB1();
        break;
    case CheckBoxActions.DoJustCB2:
        DoJustCB2();
        break;
    case CheckBoxActions.DoCB1AndCB2ButNeverCB4:
        DoCB1AndCB2ButNeverCB4();
        break;
}

Dictionary<CheckboxActions, Action> .

, , , CheckboxActions.CheckBox1 CheckboxActions.CheckBox4. , CheckboxActions , int.


, , , enum . , , ( , ):

[Flags]
enum CheckboxActions
{
    None = 0,

    CheckBox1 = 1,
    CheckBox2 = 2,
    CheckBox3 = 4,
    CheckBox4 = 8,

    DoJustCB1 = CheckboxActions.CheckBox1,
    DoJustCB2 = CheckboxActions.CheckBox2,
    DoCB1AndCB2ButNeverCB4 = CheckboxActions.CheckBox1 | CheckboxActions.CheckBox2
}
+2

int to Action Func ( - ), . , . .

.

int option = 0;
if(CB1.Checked) option = option | 1;
if(CB2.Checked) option = option | 2;
if(CB3.Checked) option = option | 4;
if(CB4.Checked) option = option | 8;

if(!lookup.HasKey(option))
    throw new NotSupportedException("I didn't expect that combination of options");

lookup[option]();

lookup (, a static )

lookup = new Dictionary<int,Action>();
lookup.Add(0,DoNothingNoOptionsSet);
lookup.Add(1,DoJustCB1);
lookup.Add(2,DoJustCB2);
lookup.Add(3,DoCB1AndCB2ButNeverCB4);
/* etc, for other valid options */

, , , .

+3

Source: https://habr.com/ru/post/1606234/


All Articles