简体   繁体   中英

C# FormatException Unhandled

I'm working with Microsoft Visual C# 2010 Express. I am currently trying to get my calculator to do addition and subtraction but I keep getting this error? I am using switch statements throughout the calculator.

        private void Add_Click(object sender, EventArgs e)
    {
        //Storing the number on display in variables total1 for further use
        //Making addition happen before = is clicked
        total1 = total1 + double.Parse(textDisplay.Text);
        textDisplay.Text = textDisplay.Text + Add.Text;
        theOperator = "+";

    }

    private void Subtract_Click(object sender, EventArgs e)
    {
        total1 = total1 + double.Parse(textDisplay.Text);
        textDisplay.Clear();

        theOperator = "-";

    }

    private void Equals_Click(object sender, EventArgs e)
    {
      switch(theOperator)
      {
          case "+": //Addition
                    total1 = total1 + double.Parse(textDisplay.Text);---> error in this line
                    textDisplay.Text = result.ToString();
                    total1 = 0;
                    break;

          case "-": //Subtraction
                    result = total1 - double.Parse(textDisplay.Text);--->error in this line
                    textDisplay.Text = result.ToString();
                    total1 = 0;
                    break;

On your problem lines, you have:

double.Parse(textDisplay.Text)

But in your Add_Click method you have this:

textDisplay.Text = textDisplay.Text + Add.Text;

I am hypothesizing that that your Add button label is not a number (likely it is either Add or + ). So when you run the line above, you would get something like either:

  • 1234Add
  • 1234+

This will cause an exception in when you pass it to double.Parse because this function does not accept bad input (so unless textDisplay.Text is a number, it will give an error). You would want to use double.TryParse instead if you want to test for bad input.


Here is an example on how to test for bad input:

private void Equals_Click(object sender, EventArgs e)
{
    // Remove the operator from the value we want to process.
    // It is expected this will be at the end.
    var userValue = textDisplay.Text;
    if (userValue.EndsWith(theOperator))
    {
        userValue = userValue.Substring(0, userValue.Length - theOperator.Length).Trim();
    }

    // Test for valid input.
    // Test our "userValue" variable which has the pending operator removed.
    double value;
    if (!double.TryParse(userValue, out value))
    {
        // Invalid input.
        // Alert the user and then exit this method.
        MessageBox.Show("A number was not entered.");
        return;
    }
    // If we get here, the "value" variable is a valid number.
    // Use it for calculations.

    ...

Edit

As a side note, you do have some logic issues with respect to how you are using and resetting result and total1 in your OP. I'm not going to do your homework for you, but it would be a good idea to review your usages of these variables.

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