Dino Prašo
Dino Prašo

Reputation: 611

Can't get simple if statement to work properly

The problem is that whatever I try the alert only appears if BOTH are empty. If one of them has a value it will set the resOne.Text to F.

Here is the code:

private void btn_calculate_Click(object sender, EventArgs e)
    {
        // START OF AND OPERATION
        if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")
        {
            if (valueOne.Text == "T" || valueOne.Text == "1")
            {
                if (valueTwo.Text == "T" || valueTwo.Text == "1")
                {
                    resOne.Text = "T";
                    resOne.BackColor = Color.LawnGreen;
                    resLineOne.BackColor = Color.LawnGreen;
                }
                else
                {
                    resOne.Text = "F";
                    resOne.BackColor = Color.Salmon;
                    resLineOne.BackColor = Color.Salmon;
                }
            }
            else
            {
                resOne.Text = "F";
                resOne.BackColor = Color.Salmon;
                resLineOne.BackColor = Color.Salmon;
            }
        }
        else if (valueOne.Text == "" || valueTwo.Text == "")
        {
            MessageBox.Show("Error: Empty Fields");
        }
    }

Any ideas ?

Upvotes: 0

Views: 101

Answers (4)

V-SHY
V-SHY

Reputation: 4125

This line is the main problem

if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")

I assume your comboBox.Text always set to "AND"

then when valueOne.Text EMPTY, valueTwo.Text NON-EMPTY you fulfiled the second part of OR

valueTwo.Text != ""

when valueOne.Text NON-EMPTY, valueTwo.Text EMPTY you fulfiled the first part of OR

comboBox1.Text == "AND" && valueOne.Text != ""

Therefore even either one has input, you can't go to Alert message. Should be

if (comboBox1.Text == "AND" && valueOne.Text != "" && valueTwo.Text != "")

ELSE IF proceed for expression checking only after the IF expression is FALSE

Upvotes: 1

d.moncada
d.moncada

Reputation: 17402

Try determining empty fields first. I would also use String.IsNullOrEmpty for checking empty strings

    if (String.IsNullOrEmpty(valueOne.Text) || String.IsNullOrEmpty(EmptyvalueTwo.Text))
    {
        MessageBox.Show("Error: Empty Fields");
    }        
    else
    {
        if (comboBox1.Text.equals("AND"))
        {
            if (valueOne.Text.equals("T") || valueOne.Text.equals("1"))
            {
                if (valueTwo.Text.equals("T") || valueTwo.Text.equals("1"))
                {
                    resOne.Text = "T";
                    resOne.BackColor = Color.LawnGreen;
                    resLineOne.BackColor = Color.LawnGreen;
                }
                else
                {
                    resOne.Text = "F";
                    resOne.BackColor = Color.Salmon;
                    resLineOne.BackColor = Color.Salmon;
                }
            }
            else
            {
                resOne.Text = "F";
                resOne.BackColor = Color.Salmon;
                resLineOne.BackColor = Color.Salmon;
            }   
        }
    }

Upvotes: 0

Heberda
Heberda

Reputation: 820

For a start, let's simply it.

private void btn_calculate_Click(object sender, EventArgs e)
    {
        if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")
        {
            //SO This bit assumes everything has a value in
            if (valueOne.Text == "T" || valueOne.Text == "1" || valueTwo.Text == "T" || valueTwo.Text == "1)
            {
                    resOne.Text = "T";
                    resOne.BackColor = Color.LawnGreen;
                    resLineOne.BackColor = Color.LawnGreen;
            }
            else
            {
                resOne.Text = "F";
                resOne.BackColor = Color.Salmon;
                resLineOne.BackColor = Color.Salmon;
            }
        }
        else if (valueOne.Text == "" || valueTwo.Text == "")
        {
            MessageBox.Show("Error: Empty Fields");
        }
    }

How are you setting the text box's? That could be your issue. If they are not exactly T or 1 that will cause an issue. It might be worth trying the Trim() method to make sure there are no white spaces. It's not an issue with the IF statement.

If it sets the text box to "F" then there is nothing wrong with the following:

if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")

Your error occurs here:

if (valueOne.Text == "T" || valueOne.Text == "1" || valueTwo.Text == "T" || valueTwo.Text == "1)

I would suggest debugging it and stepping through :)

Upvotes: 0

Damith
Damith

Reputation: 63105

if (valueOne.Text == "" || valueTwo.Text == "")

   MessageBox.Show("Error: Empty Fields");
}
else{
   // do something 

}

Upvotes: 0

Related Questions