3

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 aRadio as public or something? If so, how would this be done?

Devator
  • 3,686
  • 4
  • 33
  • 52

4 Answers4

21

You're declaring aRadio as a local variable within your constructor. You need to declare it as an instance variable, and just assign it a value within your constructor:

// 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 item rather than the selected index... there's no need to keep text/value pairs like this.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2
   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)]);
    } 
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
1

you're to access to a variable defined in the domain of your contructor form1 so you could do this

//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)]);
    }
}
Jorge
  • 17,896
  • 19
  • 80
  • 126
-3

System.StringSplitOptions.RemoveEmptyEntries : typed out in this manner will solve your problem.

Don't ask me why I just know it works this way.