0

I'm trying to build a simple calculator application in C# and I have no idea why it crashes when I do the following steps.

  1. Enter 0.2
  2. Click Subtraction
  3. Enter 0

The application crashes immediately. I assume it is something to do with the Zero() function since that is what is executed when the Zero button is clicked. The conditional statements are meant to take care of instances where shouldn't occur like consecutive ciphers and so on. Here is the source code. The functions for the other digits are identical, by the way.

    public partial class MainWindow : Window
    {
        protected double firstNumber, secondNumber;
        protected string textBoxContents;
        protected int selectedFunction;
        public MainWindow()
        {
            InitializeComponent();
            firstNumber = 0;
            secondNumber = 0;
            selectedFunction = 0;
            textBoxContents = "0";
        }
        private void Zero(object sender, RoutedEventArgs e)
        {
            if (Convert.ToDouble(textBoxContents) > 0 || textBoxContents[textBoxContents.Length - 1] == '.')
            {
                if(selectedFunction != 0)
                    textBoxContents = textBoxContents + "0";
            }
            else if (textBoxContents == null)
            {
                textBoxContents = textBoxContents + "0";
            }
            ResultBox.Content = textBoxContents;
        }
        private void One(object sender, RoutedEventArgs e)
        {
            textBoxContents = textBoxContents + "1";
            ResultBox.Content = textBoxContents;
        }
        private void Decimal(object sender, RoutedEventArgs e)
        {
            textBoxContents = textBoxContents + ".";
            ResultBox.Content = textBoxContents;
        }
        private void Addition(object sender, RoutedEventArgs e)
        {
            firstNumber = Convert.ToDouble(textBoxContents);
            textBoxContents = null;
            selectedFunction = 1;
        }
        private void Subtraction(object sender, RoutedEventArgs e)
        {
            firstNumber = Convert.ToDouble(textBoxContents);
            textBoxContents = null;
            selectedFunction = 2;
        }
        private void Multiplication(object sender, RoutedEventArgs e)
        {
            firstNumber = Convert.ToDouble(textBoxContents);
            textBoxContents = null;
            selectedFunction = 3;
        }
        private void Division(object sender, RoutedEventArgs e)
        {
            firstNumber = Convert.ToDouble(textBoxContents);
            textBoxContents = null;
            selectedFunction = 4;
        }
        private void Result(object sender, RoutedEventArgs e)
        {
            secondNumber = Convert.ToDouble(textBoxContents);
            double thirdNumber = 0;
            switch (selectedFunction)
            {
                case 1:
                    thirdNumber = firstNumber + secondNumber;
                    break;
                case 2:
                    thirdNumber = firstNumber - secondNumber;
                    break;
                case 3:
                    thirdNumber = firstNumber * secondNumber;
                    break;
                case 4:
                    thirdNumber = firstNumber / secondNumber;
                    break;
                default:
                    break;
            }
            textBoxContents = Convert.ToString(thirdNumber);
            ResultBox.Content = textBoxContents;
        }
        private void ClearEverything(object sender, RoutedEventArgs e)
        {
            textBoxContents = null;
            firstNumber = 0;
            secondNumber = 0;
            selectedFunction = 1;
            ResultBox.Content = Convert.ToString(0);
        }
        private void ToggleNegative(object sender, RoutedEventArgs e)
        {
            if (Convert.ToDouble(textBoxContents) != 0)
            {
                textBoxContents = Convert.ToString(Convert.ToDouble(textBoxContents) * -1);
                ResultBox.Content = textBoxContents;
            }
            else
                ResultBox.Content = Convert.ToString(0);
        }
    }
Siddharth
  • 1,146
  • 3
  • 15
  • 28

4 Answers4

1

The decimal separator is localized, are you sure that you are using the right culture ("," instead of ".")?

If that's the issue, check out this Stack Question

Community
  • 1
  • 1
gmanolache
  • 497
  • 4
  • 12
1
private void Zero(object sender, RoutedEventArgs e)
{
    if (Convert.ToDouble(textBoxContents) > 0 ||
        textBoxContents[textBoxContents.Length - 1] == '.')
    {
        if(selectedFunction != 0)
            textBoxContents = textBoxContents + "0";
    }
    else if (textBoxContents == null)
    {
        textBoxContents = textBoxContents + "0";
    }
    ResultBox.Content = textBoxContents;
}

That logic seems a tidge off. If the value of the text box is empty then it's going to blow up because of the indexer on the other side of the ||. I think this can be rewritten to say:

private void Zero(object sender, RoutedEventArgs e)
{
    var dblVal = Convert.ToDouble(textBoxContents.Text);
    textBoxContents.Text = dblVal.ToString();
    ResultBox.Content = textBoxContents.Text;
}

In other words, if the text box is empty the conversion will yield 0.0; if it ends in a 1. it will yield 1.0; if it's .5 it will yield 0.5. Just leverage the Convert.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Thank you so much! I had no clue that `Convert` could be leveraged in that manner. Any other tips on how I could further optimize the code? – Siddharth Dec 11 '13 at 08:37
1

textBoxContents is null after clicking on the substraction button. Instead of textBoxContents = null; use textBoxContents = "0"; or textBoxContents = string.Empty;. Why do you set it to null anyway?

Calling textBoxContents.Length in your Zero method causes a NullReferenceException.

As others mentioned before your logic in Zero() seams a bit circuitous and and certainly could be smaller.

Thorakas
  • 135
  • 5
  • I was setting it to `null` because in the head that is what an empty `String` would be. I just realised the difference between empty and `null`. – Siddharth Dec 11 '13 at 08:39
0

in Subtraction function you are doing

textBoxContents = null;

and then in zero you have

textBoxContents[textBoxContents.Length - 1]

thats why it crashes

you should check for null before any operation on textBoxContents

akbar ali
  • 427
  • 2
  • 6