Review
It seems that you are trying to make a simple calculator with number comparison in C#. There are some problems in this implementation.
ToInt32
:
Do you expect the input of calculation is always in int
format? How
about floating numbers?
Nah
method:
NamingNaming:
I have no idea how's the name
Nah
imply the operation in the method. Something likerun
orperform
are better thanNah
.Integer DivisionInteger Division:
Notice that the execution of
3 / 2
in this implementation is1
. It is a kind of integer division. Is the integer division result the correct output you expected?MessageBox.Show
PartMessageBox.Show
Part:In C# 6, you can use string interpolation and make
MessageBox.Show("Sum of thenumbers entered is: " + (a + b));
intoMessageBox.Show($"Sum of thenumbers entered is: {(a + b)}");
. Interpolated strings are useful in string displaying and it's commonly used withConsole.WriteLine
orMessageBox.Show
.
Error Handling
Null or empty string:
You use
textBox1.Text
andtextBox2.Text
as a way to read numbers from user input. How about the case of empty strings (user click buttonCheck
directly without type any character)? IftextBox.Text
passes inConvert.ToInt32
directly and the content is empty,System.FormatException
will occur. MaybeString.IsNullOrEmpty
check can be used here.-
DivideByZeroException Class can be used to handle an attempt to divide an number by zero.