2

I have simple calculator in C#.NET WF. But when I insert to my editBox something else than number, my program crash. How should I do to avoid this crash?

private void doMath()
{
    int n1 = Int32.Parse(editBox1.Text);
    int n2 = Int32.Parse(editBox2.Text);

    String o = operation.Text;

    switch(o)
    {
        case "+": result.Text = (n1 + n2).ToString();
            break;
        case "-": result.Text = (n1 - n2).ToString();
            break;
        case "*": result.Text = (n1 * n2).ToString();
            break;
        case "/": result.Text = (n1 / n2).ToString();
            break;
        case "%": result.Text = (n1 % n2).ToString();
            break;
        default: result.Text = "No operator selected.";
            break;
    }
}//end doMath

private void editBox1_TextChanged(object sender, EventArgs e)
{
    doMath();
}
1

4 Answers 4

2

Int32.Parse relies on the fact that you have an int to begin with. So trying to parse 123 will work, but trying to parse 123e will throw an exception.

Using Int32.TryParse will ask for an output int but will take care of the ugly exceptions if you pass something that isn't an int, and the return of the operation will tell you if it was successful or not. If it was, you got your value in the out parameter, if not, it'll have the default value for the type (0 in our case).

int i;
bool b1 = Int32.TryParse("123", out i);  // i = 123, b = true;
bool b2 = Int32.TryParse("123e", out i); // i = 0, b = false;
i = Int32.Parse("123"); // i = 123
i = Int32.Parse("123e"); // you got an ugly exception
2

Try this:

int n1 = 0;
Int32.TryParse(editBox1.Text,out n1);
int n2 = 0;
Int32.TryParse(editBox2.Text,out n2);
1

Alternatively you can catch this exception:

try{

    int n1 = Int32.Parse(editBox1.Text);
    int n2 = Int32.Parse(editBox2.Text);
}catch(FormatException e){
    result.Text = e.toString() + "\nEnter numerical value only";
}
2
  • But don't use exception handling if you can use int.TryParse. Invalid user input is not an exceptional case. Commented Jun 1, 2015 at 11:33
  • Sure thing. It's just an alternative where for some reason you can't use int.TryParse.
    – iobleed
    Commented Jun 1, 2015 at 11:35
0

Several answers here are correct, but I'd add a little more description:

int firstNum;
Var isFirstNumeric = Int32.TryParse(editBox1.Text, out firstNum);
int secondNum;
Var isSecondNumeric = Int32.TryParse(editBox2.Text, out secondNum);

Now you could use firstNum and secondNum as you use n1 and n2 in your original example, but you should test for validity first:

if(!isFirstNumeric)
{ 
    ShowErrorMsg("The first value is not a valid number!");
}
else if(!isSecondNumeric)
{ 
    ShowErrorMsg("The second value is not a valid number!");
}
else
{
    // We know both numbers are valid.
    // Place your switch here and calculate as before...
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.