C# 在字符串类型文本框中设置int值

C# 在字符串类型文本框中设置int值,c#,C#,我想在变量中获取一个“int”值,然后在文本框中设置它。此代码的第二行显示错误: 此表达式不能用作赋值目标 我怎样才能解决它 int nextCode = teacherManagerObj.GetCode(); //shows error "This expression cannot be used as an assignment target" Convert.ToInt32(codeTextBox.Text) = nextCode; 您正在尝试将codeTextBox的Tex

我想在变量中获取一个“int”值,然后在文本框中设置它。此代码的第二行显示错误:

此表达式不能用作赋值目标

我怎样才能解决它

int nextCode = teacherManagerObj.GetCode();

//shows error "This expression cannot be used as an assignment target"
Convert.ToInt32(codeTextBox.Text) = nextCode;   

您正在尝试将
codeTextBox
Text
属性转换为Int32,并尝试将Text属性分配为Int32,但它需要一个字符串,您不应该尝试将TextBox的
Text
属性转换为Int32,因为这是不可能的。您应该尝试将Int32变量转换为字符串,并将其分配给
codeTextBox
Text
属性

改变

  int nextCode = teacherManagerObj.GetCode();
  Convert.ToInt32(codeTextBox.Text) = nextCode;
致:

或:


Convert.ToString(nextCode)之间的差异
nextCode.ToString()
,是第一个处理
null
值。第二个。

您正在尝试将
codeTextBox
Text
属性转换为Int32,并尝试将Text属性分配为Int32,而这需要一个字符串,您不应该尝试将TextBox的
Text
属性转换为Int32,因为这是不可能的。您应该尝试将Int32变量转换为字符串,并将其分配给
codeTextBox
Text
属性

改变

  int nextCode = teacherManagerObj.GetCode();
  Convert.ToInt32(codeTextBox.Text) = nextCode;
致:

或:


Convert.ToString(nextCode)之间的差异
nextCode.ToString()
,是第一个处理
null
值。第二个。

“nextCode”已经是“int”类型。那么为什么要在“int”中再次转换它呢???此代码不工作,兄弟。@Ikr刷新页面。我写得太快了。我改变了答案“nextCode”已经是“int”类型。那么为什么要在“int”中再次转换它呢???此代码不工作,兄弟。@Ikr刷新页面。我写得太快了。我改变了答案
codeTextBox.Text = nextCode.ToString();
int nextCode = teacherManagerObj.GetCode();
codeTextBox.Text = nextCode.ToString();