1

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());
}
1

5 Answers 5

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I understand your answer and thank you for that but like what you've said, I tried removing your said .ToString() but it said Cannot implicitly convert type 'int' to 'string' But, overall it works like a charm! Thanks
But, is it really meant to I put the int intTranspo=0,intBoxDaily=0; in the two different public void? Isn't that kind of redundant or it is just the way it is?
Yep, you have to declare those variable before use. Since you are passing them to the TryParse method as out params.
2

Remove toString at the end of statement.

textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text))

Comments

1

Maybe it should be:

textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text)).ToString();

1 Comment

care to add some explanation, why this and why not OP's
1

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";
}

Comments

1

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()

1 Comment

care to add some explanation, why this and why not OP's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.