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.
- Enter 0.2
- Click Subtraction
- 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);
}
}