Visual C# - 错误 1 名称'a'当前上下文中不存在
private void button6_Click(object sender, EventArgs e)
{
for (int i = 0; i < a.Length; i++)
{
MessageBox.Show(a[i]);
}
}
public void button7_Click(object sender, EventArgs e)
{
string[] a = { textBox1.Text};
}
private void button6_Click(object sender, EventArgs e)
{
for (int i = 0; i < a.Length; i++)
{
MessageBox.Show(a[i]);
}
}
public void button7_Click(object sender, EventArgs e)
{
string[] a = { textBox1.Text};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
a
是一个方法变量;它仅在每次调用button7_Click
时存在。我怀疑您需要将其设为 *field:然后:选择一个比
a
更好的名称。a
is a method variable; it only exists per call tobutton7_Click
. I suspect you need to make it a *field:and then: choose a better name than
a
.因为 a 被定义为 button7_click 函数的局部变量,所以它在表单上是全局的;在表单的变量上定义它
Because a is defined as a local variable for the button7_click function make it global over the form; define it on the variables of the form
a
不在button6 单击的范围内。您必须在该处理程序中将其声明为一个字段,或者将其传递到自定义事件参数中。
你也可以这样做
a
is not in scope inside the button6 click.You must declare it as a field, within that handler or pass it in within a custom eventargs.
You could do this also
您在 button6_Click 中调用“a.Length”,但该方法中未定义 a 。如果您在两种方法中都需要“a”,则需要将其设为类变量。但从表面上看,您也可以在方法button6_Click 中获取文本框的内容。
you're calling "a.Length" in button6_Click but a is not defined in that method. if you need "a" in both methods you need to make it a class variable. but by the looks of this you could just as well get the contents of the textbox in method button6_Click.