Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# bool statement throws strange exception for seemingly unconnected double.parse(string)

I'm an absolute beginner when it comes to C#. Trying to learn via examples. So I've found myself a nice little calculator tutorial. Everything goes fine up to last moment, the code is working, but it doesn't take multi-digit input like 33. There's a bool statement there for turning arithmetic operations on/off and tutorial instructor figured, that we should put bool = false before the number input/button press (in button_Click).

His code looks like this:

public partial class MainWindow : Window
{
    double value = 0;
    string operation = "";
    bool operation_pressed = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        if ((tb.Text == "0") || (operation_pressed == true))
           tb.Clear();

        operation_pressed = false;
        Button b = (Button)sender;
        tb.Text += "\n" + b.Content.ToString();
    }

    private void operator_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        operation = b.Content.ToString();
        value = double.Parse(tb.Text);
        operation_pressed = true;
        equation.Content = value + " " + operation;
    }

    private void result_Click(object sender, RoutedEventArgs e)
    {

        equation.Content = "";
        switch(operation)
        {
            case "+":
                tb.Text = "\n" + (value + double.Parse(tb.Text)).ToString();
                break;
            case "-":
                tb.Text = "\n" + (value - double.Parse(tb.Text)).ToString();
                break;
            case "*":
                tb.Text = "\n" + (value * double.Parse(tb.Text)).ToString();
                break;
            case "/":
                tb.Text = "\n" + (value / double.Parse(tb.Text)).ToString();
                break;
            default:
                break;
        }
    }

    private void CE_Click(object sender, RoutedEventArgs e)
    {
        tb.Text = "\n 0";
    }

    private void C_Click(object sender, RoutedEventArgs e)
    {
        tb.Clear();
        equation.Content = "";
        value = 0;
    }
}

It compiles nicely. But when I try to input a multidigit number and follow it with a mathematical operator, it throws an exception for value = double.Parse(tb.Text); that states:

When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime object.

I'm so confused right now. There's no DateTime even involved! And I'm 100% positive, everything is like in the tutorial. What's going on? :/

Any help will be appreciated greatly!

EDIT

Screenshot of actual error:

The error

like image 257
roonroon Avatar asked Jun 12 '15 13:06

roonroon


1 Answers

First of all, you're interpreting the debugger incorrectly. This is not the error message:

When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime object.

Notice how it's listed as "Troubleshooting Tips". In the vast majority of cases, you can ignore it. The error message itself is in a language I don't know, so I can't speak to what it says. But a FormatException essentially means that you're trying to parse a value which can't be parsed.

Your screen shot cuts off some information, but what is the value of tb.Text? Is it one of those "+" strings? If so, then that's your problem.

"+" can't be parsed as a numeric value, because "+" isn't a number.

You can make your code a little more resistant to errors by using TryParse instead of Parse. Something like this:

double result;
if (!double.TryParse(tb.Text, out result))
{
    // couldn't parse
}

If the if block isn't entered, then result will contain the successfully parsed value. If it is entered, then the value couldn't be parsed. How you handle that situation is up to you. An error message to the user, a default value instead of the parsed value, etc. That's application logic for you to define.

The point is, tb.Text contains a non-numeric value which you're trying to convert into a numeric value. Hence the error.

like image 90
David Avatar answered Sep 30 '22 01:09

David