Note that you are using integers only, and the results of any division will be an integer, and so you will
lose any fractional result: 5 / 2 => 2
I'd encourage you to change the Type of your arguments/return value to 'double.
The problem with returning an integer when
no RadioButton is Checked is that some of your numeric operations may result in the
same integer you choose to return if no RadioButton is Checked: how can you know if the #0 that came back from your 'Calc function is the result of RadioButton2 being Checked and #100 is subtracted from #100, or is a result of no RadioButton being Checked ?
To avoid this I suggest you make the return value a nullable Type: [
^]
public int? calc()
{
int? result = null;
int i1 = Convert.ToInt32(textBox1.Text);
int i2 = Convert.ToInt32(textBox2.Text);
if (radioButton1.Checked)
{
result = i1 + i2;
}
else if (radioButton2.Checked)
return result;
}
So if you call 'Calc with no RadioButton checked, and the string "100" in both TextBoxes, you can test for the null result, and handle it in whatever way you want to:
radioButton2.Checked = true;
int? test = calc();
if(test != null)
{
}
else
{
}
The other thing you should consider is doing error checking if the user enters any incorrect value, or there's no text, inside the TextBoxes. That's called 'Validation, and there are several ways to approach that. Here's one:
int? result = null;
int i1, i2;
if (!(Int32.TryParse(textBox1.Text, out i1) && Int32.TryParse(textBox2.Text, out i2))) return result;
You should also test for a divide by zero error.