简体   繁体   English

C#表单应用程序int到字符串错误

[英]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 . textBox2Text属性的类型为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.

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