Home > Software design >  Why this last condition else doesn't work properly C#
Why this last condition else doesn't work properly C#

Time:04-25

I have this (if-else if-else) statement it works fine except the else part I don't know why ?

void TakeAction()
{
    try
    {
        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {
            InsertProduct();
            MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        else if (chkProduct.Checked == false && chkMaterial.Checked == true)
        {
            InsertMaterial();
            MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Close();
        }

        else if (chkProduct.Checked == true && chkMaterial.Checked == true)
        {
            InsertSubProduct();
            MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Close();
        }

        // else if (chkProduct.CheckState == CheckState.Unchecked && chkMaterial.CheckState == CheckState.Unchecked) // Tried this also still nothing
        else
        //Doesn't work
        {
            MessageBox.Show("Check even one Checkbox", "Choose", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
    catch (Exception Err)
    {
        MessageBox.Show("This Error Occured :"   Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

I tried different ways like just else , else if And finally the commented line in code . Thanks in advance.

Edit#1 I think this method interferes with the TakeAction method and causes the problem , I will debug my code from the start , Thanks

        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {
            da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
            da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
            dt.Clear();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                //int cnt = Convert.ToInt32(dt.Rows[0]["ProductCode"].ToString());
                MessageBox.Show("Duplicated", "Duplicated");

            }
            else
            {
                //MessageBox.Show("No problem , Its not Product");
                TakeAction();
            }
        }

        //the first If >> If Material Or SubProduct
        else if (chkMaterial.Checked == true)
        {
            da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
            da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
            dt.Clear();

            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("Duplicated", "Duplicated");

            }
            else
            {
                // MessageBox.Show("No problem , Its not item");
                TakeAction();
            }

CodePudding user response:

You have 4 different combinations in total:

chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
              true :                true : InsertSubProduct()
              true :               false : InsertMaterial()
             false :                true : InsertProduct()
             false :               false : Ask User to Check         

That's why you don't have to put the last condition as else if: else is enough. If you have problems with debugging you can change the code into nesting ifs and push the last condition forward:

if (!chkProduct.Checked && !chkMaterial.Checked) 
  MessageBox.Show("Check even one Checkbox", 
                  "Choose", 
                   MessageBoxButtons.OK, 
                   MessageBoxIcon.Warning);
else {
  if (chkProduct.Checked)
    if (chkMaterial.Checked) 
      InsertSubProduct();
    else 
      InsertProduct();
  else 
    InsertMaterial();

  MessageBox.Show("Done", 
                  "Done", 
                   MessageBoxButtons.OK, 
                   MessageBoxIcon.Information);
  Close();
}

CodePudding user response:

If the else statement doesn't work it means that one of the else if statements is being hit. Have you tried breakpointing? You can also remove the true and false values to make it more readable.

Instead of:

if (x == true && y == false) { }

You can do the following:

if (x && !y) { }
  • Related