Reputation: 91
I'm building this three textbox where if the two textbox are filled the sum will be displayed however, when I input 10 + 10. The result is 1010. Can anbody help me with this?
Here's my code:
public void textBoxTranspo_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text).ToString());
}
public void textBoxDaily_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text).ToString());
}
Upvotes: 1
Views: 473
Reputation: 3618
Do it like this:
int ans = 0;
ans = Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text);
textBoxTotalAmount.Text = ans.ToString();
EDIT:
This is just one way of creating a clean/neat and more readable code. The way other answered like this
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text)).ToString();
is also correct but if other programmers will read the code, the 1st one I wrote is more readable and in this case more efficient.
You're getting 1010 because you misplaced the .ToString()
Upvotes: 1
Reputation: 460
to avoid confusion, first, try to convert the values of the textboxes into int, and put it in respective variables if successful, and output an invalid prompt if not.
int input1 = 0;
int input2 = 0;
try
{
input1 = Convert.ToInt32(textBoxTranspo.Text);
input2 = Convert.ToInt32(textBoxDaily.Text);
ans = input1 + input2;
if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
{
textBoxTotalAmount.Text = ans.ToString();
}
}
catch (Exception)
{
textBoxTotalAmount.Text = "Invalid input";
}
Upvotes: 1
Reputation: 23144
Remove toString
at the end of statement.
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text))
Upvotes: 2
Reputation: 29026
It is the magic(polymorphism) of the +
operator. it will add the values of two operands if the operands are of numeric types(int,long, double) and it will concatenate two operands if they are of type strings or even one string and second integer(as like in your case). Here in your case the .ToString()
after convert creates the issues. You will get the expected result by removing that from that line.
An additional note : Convert.ToInt32
will throw FormatException
if the input text is not convertible, so use have to use int.TryParse
for converting text to integer. so the code will looks like this:
int intTranspo=0,intBoxDaily=0;
if(int.TryParse(textBoxTranspo.Text,out intTranspo) && int.TryParse(textBoxDaily.Text,out intBoxDaily))
textBoxTotalAmount.Text = (intTranspo + intBoxDaily).ToString();
Upvotes: 2
Reputation: 6053
Maybe it should be:
textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text)).ToString();
Upvotes: 1