Bool and indeterminate checkbox

Berbob13a

New member
Joined
Dec 19, 2021
Messages
1
Programming Experience
5-10
What is the preferred syntax for coding a forms app with a checkbox that can be ticked (true) or not (false). As there exists also the indeterminate state.

A task in a course required bool to be sent to a overloaded method. This code works, but is it the best code? Asking as the course material is buggy.
C#:
        private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
        {
            if (_checkBoxFastAnstalld.CheckState == CheckState.Checked)
            {
                Personinfo(Convert.ToInt32(_textBoxAlder.Text), _checkBoxFastAnstalld.Checked);
            }
            else if (_checkBoxFastAnstalld.CheckState == CheckState.Indeterminate)
            {
                Personinfo(Convert.ToInt32(_textBoxAlder.Text), false);
            }
            else
            {
                Personinfo(Convert.ToInt32(_textBoxAlder.Text), _checkBoxFastAnstalld.Checked);
            }
        }
 
Last edited by a moderator:
C#:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = _checkBoxFastAnstalld.Checked && _checkBoxFastAnstalld.CheckState != CheckState.Indeterminate;
    Personinfo(int.Parse(_textBoxAlder.Text), state);
}
 
Though more elaborate than Skydiver's terse and efficient solution, I would prefer this. Much easier to see what happens when, and easier to change if necessary.

C#:
        private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
        {
            switch ( _checkBoxFastAnstalld.CheckState )
            {
                case CheckState.Checked:
                    Personinfo(int.Parse(_textBoxAlder.Text), true);
                    break; 
                case CheckState.Unchecked:
                    Personinfo(int.Parse(_textBoxAlder.Text), false);
                    break;
                case CheckState.Indeterminate:
                    Personinfo(int.Parse(_textBoxAlder.Text), false);
                    break;                 
            }
        }

Apparently this is a three-state checkbox, or else this code would not be needed.
But isn't it funny that when CheckState is Indeterminate, Checked is true rather than false ? I can't think of a reason why this should be so.
 
CheckState is Indeterminate, Checked is true rather than false ? I can't think of a reason why this should be so.
Often this is used to show that some but not all sub-options is checked, Support here is "partially checked"
1640687943901.png
 
Though more elaborate than Skydiver's terse and efficient solution, I would prefer this. Much easier to see what happens when, and easier to change if necessary.
Refactoring out the duplicated code:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = false;
    switch (_checkBoxFastAnstalld.CheckState)
    {
        case CheckState.Checked:
            state = true;
            break;

        case CheckState.Unchecked:
        case CheckState.Indeterminate:
        default:
            state = false;
            break;
    }
    
    Personinfo(int.Parse(_textBoxAlder.Text), state);   
}

followed by another refactoring:
Refactoring the switch to if:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = false;
    if (_checkBoxFastAnstalld.CheckState == CheckState.Checked)
        state = true;
    else
        state = false;
    Personinfo(int.Parse(_textBoxAlder.Text), state);   
}

and then refactoring further:
Refactoring out the if that just goes into a boolean:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = _checkBoxFastAnstalld.CheckState == CheckState.Checked;
    Personinfo(int.Parse(_textBoxAlder.Text), state);
}

and refactoring to remove the pass-thru variable:
Refactoring the one time use variable:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    Personinfo(int.Parse(_textBoxAlder.Text),
               _checkBoxFastAnstalld.CheckState == CheckState.Checked);
}
 
Often this is used to show that some but not all sub-options is checked, Support here is "partially checked" View attachment 1933
Refactoring out the duplicated code:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = false;
    switch (_checkBoxFastAnstalld.CheckState)
    {
        case CheckState.Checked:
            state = true;
            break;

        case CheckState.Unchecked:
        case CheckState.Indeterminate:
        default:
            state = false;
            break;
    }
   
    Personinfo(int.Parse(_textBoxAlder.Text), state);  
}

followed by another refactoring:
Refactoring the switch to if:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = false;
    if (_checkBoxFastAnstalld.CheckState == CheckState.Checked)
        state = true;
    else
        state = false;
    Personinfo(int.Parse(_textBoxAlder.Text), state);  
}

and then refactoring further:
Refactoring out the if that just goes into a boolean:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    bool state = _checkBoxFastAnstalld.CheckState == CheckState.Checked;
    Personinfo(int.Parse(_textBoxAlder.Text), state);
}

and refactoring to remove the pass-thru variable:
Refactoring the one time use variable:
private void _buttonSameMethod2ParamsDifferent_Click(object sender, EventArgs e)
{
    Personinfo(int.Parse(_textBoxAlder.Text),
               _checkBoxFastAnstalld.CheckState == CheckState.Checked);
}
Good work, and brownie points for the most terse version. Though it is under the assumption that whatever the case, you ONLY want to call Personinfo(). I'd stick with my version for scalabilty. Just in case one of the states should need additional or different actions.
 
I used to think that way too, until YAGNI got rammed down my throat.
 
I used to think that way too, until YAGNI got rammed down my throat.
I had not heard of YAGNI - it seems to make sense as does KISS. Still, I think a creative one-liner may take more time, both to conceive and understand, than stupidly spelling things out with a switch. It's a matter of preference. And I happen to like the accountant-like demeanor of the switch statement 😁
 
Back
Top Bottom