简体   繁体   English

'int'不包含'Text'的定义

[英]'int' does not contain a definition for 'Text'

I know what a simple problem this is, but for the life of me I can't find the solution elsewhere. 我知道这是一个简单的问题,但是对于我一生来说,我找不到其他解决方案。 Here is my code: 这是我的代码:

int txtAge = 20;
txtAge.Text = txtAge.ToString();

The error occurs specifically for .Text . 该错误专门针对.Text发生。 The TextBox object and the variable are both named txtAge . TextBox对象和变量都命名为txtAge I know the reasonable thing to do is to give them different names, but this was done by my professor so I'm hesitant to change them. 我知道合理的做法是给他们起不同的名字,但这是我的教授做的,所以我不愿意更改它们。

You can use this to tell the compiler you want to use the non-local variable: 您可以使用this来告诉编译器您要使用非本地变量:

this.txtAge.Text = txtAge.ToString();
  • Also, note the () after ToString . 另外,请注意ToString之后的() You'll need those. 您将需要这些。

Also note that this practice is highly discouraged. 另请注意,强烈建议不要这样做。 You should avoid re-using variable names. 您应该避免重用变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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