Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Bool method keeps returning false

So this code checks to make sure the user has entered a proper math operator in my calculator, but no matter what I seem to do the code ALWAYS gives me false and it's driving me nuts.

if (IsOperator(txtOperator.ToString(), "Operator")




        public bool IsOperator(string textBox, string name)
        {
            switch (textBox)
            {
                case "*":
                    return true;
                case "x":
                    return true;
                case "/":
                    return true;
                case "+":
                    return true;
                case "-":
                    return true;
            }
                MessageBox.Show(name + " must be a valid math operator.", "Entry Error");
                return false;
        }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think you need txtOperator.Text instead of txtOperator.ToString() in the call to IsOperator, assuming txtOperator is a TextBox.

Have you already debugged the app and inspected the value?

over 4 years ago · Santiago Trujillo Report

0

Probably you just need to pass in the the Text property of that textbox...

   if (IsOperator(txtOperator.Text, "Operator")) { ...

The method itself could be optimized to sth like:

    public bool IsOperator(string textEntered, string name)
    {
        switch (textEntered?.Trim())
        {
            case "*":
            case "x":
            case "/":
            case "+":
            case "-":
                return true;

            default:
                MessageBox.Show(name + " must be a valid math operator", "Entry Error");
                return false;

        }
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!