[英]C# Form Application int to string error
I am really stuck on this one - I am trying to create an if statement and display the value assigned to the variable in a text box. 我真的很坚持这一点-我试图创建一个if语句,并在文本框中显示分配给变量的值。 I keep coming across a "Cannot implicity convert int to string" building error. 我不断遇到“无法隐式将int转换为字符串”的构建错误。 Thanks. 谢谢。
int n1;
int userInput = int.Parse(textBox1.Text);
if (userInput == 4)
{
n1 = 60;
}
else if (userInput ==3)
{
n1=40
}
{
textBox2.Text = (n1); //"Cannot implicity convert int to string"
}
You have declared n1
as int
. 您已将n1
声明为int
。 The Text
property of textBox2
is of type string
. textBox2
的Text
属性的类型为string
。 So you first have to convert n1
to string
before you can assign it to the Text
. 因此,首先必须将n1
转换为string
然后才能将其分配给Text
。
textBox2.Text = n1.ToString();
该错误是不言自明的-编译器无法自动将整数转换为字符串:
textBox2.Text = n1.ToString();
使用方法ToString()
textBox2.Text = n1.ToString();
You have to convert int to string 您必须将int转换为字符串
textBox2.Text=n1.TOString();
or 要么
textBox2.Text=Convert.ToString(n1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.