0
  public bool IsOperator(TextBox textbox, string name)
        {
            if (name != "+" || name != "-" || name != "*" || name != "/")
            {
                MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
                txtOperator1.Focus();
                return false;
            }
            return true;
        }

I am making a basic calculator and I have to validate that the operand entered into the text box is either a +, -, *, or /. I coded this as a generic validating method have correctly called it in my program. However, when I run the program and enter any of the operands that should be valid it pops up and tells me they're not. I'm really stuck and would greatly appreciate any help.

Edit: Here is the other parts of my code that goes with this validation. The other validators are working fine it's just the IsOperator that is giving me grief.

private void btnCalculate_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (IsValidData())
                {
                    decimal operand1 = Convert.ToDecimal(txtOperand1);
                    string operator1 = Convert.ToString(txtOperator1);
                    decimal operand2 = Convert.ToDecimal(txtOperand2);

                    decimal result = Calculate(operand1, operator1, operand2);

                    txtResult.Text = result.ToString("n4");
                    txtOperand1.Focus();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception");
            }
        }

public bool IsValidData()
        {
            
                IsPresent(txtOperand1, "Operand 1") &&
                IsDecimal(txtOperand1, "Operand 1") &&
                IsWithinRange(txtOperand1, "Operand 1", 1, 999999) &&

                IsPresent(txtOperator1, "Operator") &&
                IsOperator(txtOperator1, "Operator") &&

                IsPresent(txtOperand2, "Operand 2") &&
                IsDecimal(txtOperand2, "Operand 2") &&
                IsWithinRange(txtOperand2, "Operand 2", 1, 999999);
        }

private decimal Calculate(decimal operand1, string operator1, decimal operand2)
        {
            //This method performs the calculation based on what operand was entered 
            if (txtOperator1.Text == "+")
            {
                decimal calculationResult =  operand1 + operand2;
                return calculationResult;
            }

            else if (txtOperator1.Text == "-")
            {
                decimal calculationResult = operand1 - operand2;
                return calculationResult;
            }

            else if (txtOperator1.Text == "*")
            {
                decimal calculationResult = operand1 * operand2;
                return calculationResult;
            }

            else 
            {
                decimal calculationResult = operand1 / operand2;
                return calculationResult;
            }
            
        }

2
  • 11
    You need to use && (and) instead of || (or) Commented May 11, 2015 at 18:14
  • 1
    This (name != "+" || name != "-" || name != "*" || name != "/") is always true Commented May 11, 2015 at 18:23

3 Answers 3

2

You need to reverse your if statement logic

public bool IsOperator(TextBox textbox,string name)
    {
        if (textbox.Text== "+" || textbox.Text == "-" || textbox.Text == "*" || textbox.Text == "/")
        {
          return true;
        }
            MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
            txtOperator1.Focus();
            return false;            
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick reply! It makes sense and I feel like it should work but for some reason it isn't :( This is how I called it in my IsValidData method if this helps. IsOperator(txtOperator1, "Operator") Let me know if posting any other parts of my code would help.
Also try to setup a break-point on this function and check what is being passed to it.
I edited the original post with the other parts of the code that I think are the relevant parts. Thanks again for the help.
0

You are using || witch is Or so if it is not equal to + or - or * or / it will show you the message box. so if you do + it is not equal to - min so it will still show it.

Comments

0

You should use && (AND) instead of || (OR). When you're using || (OR), when one of those conditions is true, then the whole condition is true. So when name = "+", the first part of your if statement (name != "+") evaluates false, but all of the other parts evaluate true, and only one needs to evaluate true for the entire if statement to be true.

public bool IsOperator(TextBox textbox, string name)
{
    if (name != "+" && name != "-" && name != "*" && name != "/")
    {
        MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
        txtOperator1.Focus();
        return false;
    }
    return true;
}

Using the && (AND) operator, all conditions must be true for the whole condition to be true.

You could also try the following, which avoids using && (AND) and || (OR)...

private string validOperators = "+-*/";
public bool IsOperator(TextBox textbox, string name)
{
    if (!validOperator.Contains(name))
    {
        MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
        txtOperator1.Focus();
        return false;
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.