Tutorial :When a form loads read txt file into array



Question:

I am working on a C# application where when a form loads, I want it to read in the contents of a txt file and store it to an array. Next, when a click a button on the form, I want the button click event to access the array. How do I pass the array to the button click event? My code below has an error "statusArray does not exist in current context" and is related to the reference to the array in the button click event. What do I need to do?

Susan

private void btnCompleted_Click(object sender, EventArgs e)      {            for (int i = 0; i < statusArray.Count; i++)          {              if (statusArray[i].Equals("Complete"))                    lstReports.Items.Add(statusArray[i-2]);            }      }        private void Reports_Load(object sender, EventArgs e)      {          // declare variables          string inValue;          string data;          ArrayList statusArray = new ArrayList();              inFile = new StreamReader("percent.txt");            // Read each line from the text file            while ((inValue = inFile.ReadLine()) != null)          {              data = Convert.ToString(inValue);              statusArray.Add(inValue);            }            // Close the text file          inFile.Close();          }  


Solution:1

Store the ArrayList as a member variable on your form as follows:

private ArrayList statusArray = new ArrayList();    private void btnCompleted_Click(object sender, EventArgs e) {        for (int i = 0; i < statusArray.Count; i++)      {          if (statusArray[i].Equals("Complete"))                lstReports.Items.Add(statusArray[i-2]);        }  }    private void Reports_Load(object sender, EventArgs e)  {      // declare variables      string inValue;      string data;        inFile = new StreamReader("percent.txt");        // Read each line from the text file        while ((inValue = inFile.ReadLine()) != null)      {          data = Convert.ToString(inValue);          statusArray.Add(inValue);        }        // Close the text file      inFile.Close();      }  


Solution:2

move your arrayList declaration outside of Reports_Load(object sender, EventArgs e) method, which makes it a class global.

You might also find a List<string> would be better to store your data in (strongly typed)


Solution:3

An ArrayList is not the same thing as an Array, and if you're using .Net 2.0 or later ArrayLists are evil.

As for the reason why this fails: your arraylist is scoped to the Reports_Load() function. You want to move it to the class level, and declare as a List<string>.

Another option if you really want an array is to use the File class' .ReadAllLines() method.

private string[] status;    private void btnCompleted_Click(object sender, EventArgs e)  {      for (int i = 2; i < status.Length; i++)      {          if (status[i] == "Complete")              lstReports.Items.Add(status[i-2]);        }  }    private void Reports_Load(object sender, EventArgs e)  {      status = File.ReadAllLines("percent.txt");  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »