Basic questions while making a toy calculator
- by Jwan622
I am making a calculator to better understand how to program and I had a question about the following lines of code:
I wanted to make my equals sign with this C# code:
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + Convert.ToDouble(txtDisplay.Text);
//double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked ==
{
total2 = total1 - double.Parse(txtDisplay.Text)
}
}
txtDisplay.Text = total2.ToString();
total1 = 0;
However, my friend said this way of writing code was superior, with changes in the minus sign.
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + Convert.ToDouble(txtDisplay.Text);
//double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true)
{
double d1;
if(double.TryParse(txtDisplay.Text, out d1))
{
total2 = total1 - d1;
}
}
txtDisplay.Text = total2.ToString();
total1 = 0;
My questions:
1) What does the "out d1" section of this minus sign code mean?
2) My assumption here is that the "TryParse" code results in fewer systems crashes? If I just use "Double.Parse" and I don't put anything in the textbox, the program will crash sometimes right?