0

Z:P

Y:A B C

A:B C

C:B

B:

$:@

This is what's in my test text file. left side of the colon will go into Package column on the list view. right side of the colon will go into D1 D2 D3 ... column on the list view. I've been trying, but keep stuck at for loop. it's keep saying that index was outside the bounds of the array even when I remove the for loop and put it like

PCK.Dependency.Add(text.Split(' ')1);

sample image

    private struct Package
    {
        public string NameOfPackage;
        public List<string> Dependency;
    }

    List<Package> PackageList = new List<Package>();
    Package PCK = new Package();

    private void UI_btnLoad_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            StreamReader sr = new StreamReader(openFileDialog1.FileName);
            string text;
            while ((text = sr.ReadLine()) != null)
            {
                PCK.NameOfPackage = text.Split(':')[0];
                for (int i = 1; i < 10; i++)
                {
                    PCK.Dependency.Add(text.Split(' ')[i]);  //Problem here
                }
                PackageList.Add(PCK);
            }
            ListViewItem lvi = new ListViewItem(PCK.NameOfPackage.ToString());
            for(int i = 1; i < 10; i++)
            {
                lvi.SubItems.Add(PCK.Dependency[i].ToString());
            }
            listView1.Items.Add(lvi);

        }
    }
}

}

I have no idea what my problem is... please give me an advice ! Thank you.

Eric Hobin Yoo
  • 123
  • 1
  • 8
  • You are trying to access the index of `text.Split()` at index `[i]`. If `i` is greater than the index of `text.Split()` then you will get the error. Try to look at your code and determine how you can make sure that you are only accessing `text.Split()` from a correct index. – Sudsy1002 Feb 26 '18 at 19:45
  • As for getting the error when trying `PCK.Dependency.Add(text.Split(' ')[1]);` Try to think about what that means. You are trying to access `text.Split()` at index `1` when it is split by a space. Keep in mind that indexes are 0 based. – Sudsy1002 Feb 26 '18 at 19:47

1 Answers1

0

Beginning with the while loop you should substitute with this code. The foreach-loop ensures that you are not accessing beyond the limits of the array. Likewise when you are adding the elements to the ListView. NEVER EVER use loops with a magic number as the upper bound.

You also did the same split inside your original loop 10 times which is as useless as tieing your shoes 10 times in the morning.

        while ((text = sr.ReadLine()) != null)
        {
            PCK.NameOfPackage = text.Split(':')[0];

            string[] dependencies = text.Split(' ');
            foreach(string s in dependencies)
                PCK.Dependency.Add(s);

            PackageList.Add(PCK);
        }

        ListViewItem lvi = new ListViewItem(PCK.NameOfPackage.ToString());
        foreach(string d in PCK.Dependency)
            lvi.SubItems.Add(d);

       listView1.Items.Add(lvi);
oliver
  • 2,771
  • 15
  • 32