简体   繁体   中英

cannot implicitly convert type 'int' to 'string' in C#

I want to make a programs to sum 2 number in 2 text box but it's just not working. I don't know exactly why. (And sorry for my bad english skill XD).

Here's the code :

textBox3.Text = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);

Since the result of the operation is not a string but an int you have to call ToString() to assign the string value to textBox3.Text

textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();

Working code:

string y = "";
y = (Convert.ToInt32("3") + Convert.ToInt32("4")).ToString();

将Int32值转换为字符串以显示。

textBox3.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM