In one of my seminars, a high school student asked me what's a simple way to add two decimal numbers from the two textbox and display the result on the third textbox.
I know it's so simple and we have 'shortcuts' in doing this task. But since I found that it's a common question by some public high school studs who attended my seminars, I decided to post here a simple solution that can be easily understood by students:
decimal num1 = 0;
decimal num2 = 0;
if (txtValue1.Text != "")
{
decimal value1 = Decimal.Parse(txtValue1.Text);
num1 = value1;
}
if (txtValue2.Text != "")
{
decimal value2 = Decimal.Parse(txtValue2.Text);
num2 = value2;
}
var total = num1 + num2;
txtTotal.Text = total.ToString();
if (string.IsNullOrWhiteSpace(textBox1.Text)
ReplyDelete|| string.IsNullOrWhiteSpace(textBox1.Text)) return;
decimal val1 = Decimal.Parse(textBox1.Text);
decimal val2 = Decimal.Parse(textBox2.Text);
decimal result = val1 + val2;
textBox3.Text = result.ToString();
:D
//update :D
ReplyDeleteif (string.IsNullOrWhiteSpace(textBox1.Text)
|| string.IsNullOrWhiteSpace(textBox1.Text)) return;
decimal[] val = {Decimal.Parse(textBox1.Text), Decimal.Parse(textBox1.Text)};
textBox3.Text = val.Sum().ToString();