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?

4 Answers 4

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.

4
  • Could you elaborate yourself on the additional comments you posted? Where would you want to read files? On another thread?
    – Devator
    Commented Apr 23, 2012 at 13:41
  • @Devator: Probably on start-up in some entirely separate code, while a splash-screen is showing. It really depends on the rest of the application.
    – Jon Skeet
    Commented Apr 23, 2012 at 14:01
  • You should use Path.Combine
    – user
    Commented Sep 28, 2012 at 7:10
  • @user: Have added a "TODO" - I wanted to keep the original code close in general.
    – Jon Skeet
    Commented Sep 28, 2012 at 7:38
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)]);
    } 
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)]);
    }
}
1
  • 1
    Please don't recommend public fields like this :(
    – Jon Skeet
    Commented Apr 23, 2012 at 13:31
-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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.