简体   繁体   中英

Validating a specific string in C#

  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; } } 

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;            
    }

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM