C# 当前上下文中不存在名称 XXXX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10281478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name XXXX does not exist in the current context
提问by Devator
I have the following code:
我有以下代码:
public Form1()
{
InitializeComponent();
string strRadio = Utils.ReadFile(strTemp + @"\rstations.txt");
string[] aRadio = strRadio.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < aRadio.Length; i += 2)
{
listBox.Items.Add(aRadio[i]);
}
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBox.SelectedIndex;
MessageBox.Show(aRadio[(index+1)]);
}
Now the error is The name 'aRadio' does not exist in the current context. Which comes from MessageBox.Show(aRadio[(index+1)]);. Do I need to declare the aRadioas public or something? If so, how would this be done?
现在的错误是The name 'aRadio' does not exist in the current context。其中来自MessageBox.Show(aRadio[(index+1)]);. 我是否需要将其声明aRadio为 public 或什么?如果是这样,这将如何完成?
采纳答案by Jon Skeet
You're declaring aRadioas a local variable within your constructor. You need to declare it as an instancevariable, and just assign it a value within your constructor:
您在aRadio构造函数中声明为局部变量。您需要将其声明为实例变量,并在构造函数中为其分配一个值:
// TODO: Give this a better name
private readonly string[] aRadio;
// TODO: Give your form a better name too
public Form1()
{
InitializeComponent();
// TODO: You might want to reconsider reading files in a GUI constructor, too
// TODO: Use Path.Combine(strTemp, "rstations.txt" instead of concatenation
string strRadio = Utils.ReadFile(strTemp + @"\rstations.txt");
aRadio = strRadio.Split(new string[] { "#" },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < aRadio.Length; i += 2)
{
listBox.Items.Add(aRadio[i]);
}
}
I wouldn't be surprised if you could do rather better than this approach though, by adding a custom object (or just a KeyValuePair<string, string>) to the list box and binding the display part through properties. That way you could get the selected itemrather than the selected index... there's no need to keep text/value pairs like this.
如果您可以通过将自定义对象(或仅 a KeyValuePair<string, string>)添加到列表框并通过属性绑定显示部分,可以做得比这种方法更好,我不会感到惊讶。这样你就可以获得选定的项目而不是选定的索引......没有必要像这样保留文本/值对。
回答by Jorge
you're to access to a variable defined in the domain of your contructor form1so you could do this
您将访问在构造函数域中定义的变量,form1以便您可以执行此操作
//Define the variable as an attribute of class
private string[] strRadio;
public Form1()
{
InitializeComponent();
string strRadio = Utils.ReadFile(strTemp + @"\rstations.txt");
aRadio = strRadio.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < aRadio.Length; i += 2)
{
listBox.Items.Add(aRadio[i]);
}
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBox.SelectedIndex;
MessageBox.Show(aRadio[(index+1)]);
}
}
回答by Bob Vale
private string[] aRadio;
public Form1() {
InitializeComponent();
string strRadio = Utils.ReadFile(strTemp + @"\rstations.txt");
this.aRadio = strRadio.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < aRadio.Length; i += 2)
{
listBox.Items.Add(aRadio[i]);
}
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e) {
int index = listBox.SelectedIndex;
MessageBox.Show(this.aRadio[(index+1)]);
}
回答by Ricahrd Rindom
System.StringSplitOptions.RemoveEmptyEntries : typed out in this manner will solve your problem.
System.StringSplitOptions.RemoveEmptyEntries :以这种方式输入将解决您的问题。
Don't ask me why I just know it works this way.
不要问我为什么我只知道它是这样工作的。

