user2740978
user2740978

Reputation: 13

How to put error checks in a method?

How can I put these error checks in a method? The user cannot crash the program, the number values have to be between 0 and 1,000,000. This is what I have so far.

If there is an empty field then there has to be an error message that says: Please enter in the missing fields.

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            // changes value that is in text box and switches it into a decimal
            decimal firstNumber = Convert.ToDecimal(txtOperand1.Text);
            decimal secondNumber = Convert.ToDecimal(txtOperand2.Text);
            string mathSymbol = txtOperator.Text;   // already a string variable
            decimal result = 0;

            //calls the method
            result = calculate(firstNumber, secondNumber, mathSymbol, result);



            if (mathSymbol != "/" && mathSymbol != "*" && mathSymbol != "+" && mathSymbol != "-")
            {
                txtOperator.Text = ""; 
                MessageBox.Show("ERROR invalid operator");

            }



            if (firstNumber <= 0 || firstNumber >= 100000 || secondNumber <= 0 || secondNumber >= 1000000)
            {
                txtResult.Text = ""; 
                MessageBox.Show("Numbers must be greater than 0 and less than 1,000,000");
                txtOperand1.Text = "";
                txtOperand2.Text = "";
                txtOperator.Text = "";                
            }

        }
        catch (FormatException)
        {
            MessageBox.Show("Please enter values.", "ERROR");
        }
        catch (Exception op)
        {
            MessageBox.Show(op.Message, "ERROR");
        }
    }

    // method calculate - amount = to calculate then calculate = to result
    private decimal calculate(decimal num1, decimal num2, string symbol, decimal amount)
       {    
            if (symbol == "+")      // checks if user enters a + then adds numbers
                amount = num1 + num2;

            else if (symbol == "-") // checks if user enters a - then minus numbers
                amount = num1 - num2;

            else if (symbol == "*") // checks if user enters a * then multiplies numbers
                amount = num1 * num2;

           else if (symbol == "/")  // checks if user enters a / then divides numbers
                amount = num1 / num2;

            txtResult.Text = amount.ToString("f4");

           return amount;
       }






    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();   // closes the form if exit button is pressed
    }

    private void clearResult(object sender, EventArgs e)
    {
        txtResult.Text = " "; // clears results if txtOperand1 is changed
    }

    private void clear_result(object sender, EventArgs e)
    {
        txtResult.Text = " "; // clears results if txtOperator is changed
     }

    private void ClearResults3(object sender, EventArgs e)
    {
        txtResult.Text = " "; // clears results if txtOperand2 is changed
    }
}

Upvotes: 1

Views: 145

Answers (2)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

Here is my aproach

private int ValidInput(string operand1, string operand2, string mathSymbol, out decimal firstNumber, out decimal secondNumber out string errorMsg)
{
    int errorCode  ;

    errorMsg = string.Emtpy ;
    firstNumber = 0 ;
    secondNumber = 0 ;

    try 
    {
        if ((mathSymbol != "/") && (mathSymbol != "*") && (mathSymbol != "+") && (mathSymbol != "-"))
        {
            errorCode = 1 ;
            errorMsg = "ERROR invalid operator";
        }   
        else
        {
            errorCode = 2 ;
            firstNumber = Convert.ToDecimal(operand1);

            if (firstNumber <= 0 || firstNumber >= 1000000)
            {
                errorCode = 3 ;
                errorMsg = "first number must be greater than 0 and less than 1,000,000" ;
            }
            else
            {
                errorCode = 4 ;
                secondNumber = Convert.ToDecimal(operand2);

                if (secondNumber <= 0 || secondNumber >= 1000000)
                {   
                    errorCode = 5 ;
                    errorMsg = "second number must be greater than 0 and less than 1,000,000" ;
                }
                else
                {
                    errorCode = 0;
                }
            }

        }
    }
    catch (FormatException  fe)
    {
        errorMsg = fe.Message ;
    }
    catch (OverflowException oe)
    {
        errorMsg = oe.Message ;
    }
    return errorCode
}
private void btnCalculate_Click(object sender, EventArgs e) 
{
    decimal firstNumber ;
    decimal secondNumber ;
    string mathSymbol = txtOperator.Text;   // already a string variable
    decimal result = 0;
    int errorCode ;
            string errorMsg ;

    errorCode = ValidInput(txtOperand1.Text.Trim(), txtOperand2.Text.Trim(), mathSymbol, out firstNumber, out secondNumber out errorMsg) ;

    //if there was no error
    if(errorCode == 0)
    {
        //calls the calculate method
        result = calculate(firstNumber, secondNumber, mathSymbol, result);

        //you can use the errorCode number to decide which fields to clear or provide more usefull message
    }
    else
    {
        MessageBox.Show(errorMsg, "ERROR");
    }
}

By the way in your error message you are saying "1,000.000" but in you code you have "100000"

Upvotes: 1

Jim Mischel
Jim Mischel

Reputation: 133995

Typically you'd use Decimal.TryParse to convert and output a message if the number is invalid. For example:

decimal firstNumber;
decimal secondNumber;

if (!decimal.TryParse(txtOperand1.Text, out firstNumber))
{
    MessageBox.Show("Number is not valid.");
    return;
}
if (firstNumber <= 0 || firstNumber >= 1000000)
{
    MessageBox.Show("Number must be > 0 and < 1000000");
    return;
}

You can do something similar with the second number.

Or, just wrap the whole thing up in a method and call it, having it return a bool to indicate if the numbers are valid.

Upvotes: 2

Related Questions