简体   繁体   中英

Index was out of bounds of array? c# forms

I'm trying to read the following textfile:(skipping first 8 lines) And reading from arrow each column 在此处输入图片说明

And am doing so by putting each column value in an array which is dictated by position and length

To test if the array value actually captured a column value I want to see the value[0] when I click another button. But when I run my app, I get the error that my index was out of bounds of the array? How, when my array size is 3 and I don't go beyond that.

    string[] val = new string[3 ]; // One of the 3 arrays - this stores column values

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

            string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
            textBox1.Lines = lines;

             int[] pos = new int[3] { 3, 6,18}; //setlen&pos to read specific clmn vals
             int[] len = new int[3] {2, 10,28}; // only doing 3 columns right now



             foreach (string line in textBox1.Lines)
             {
                 for (int j = 0; j <= 3; j++)
                 {
                     val[j] = line.Substring(pos[j], len[j]); // THIS IS WHERE PROBLEM OCCURS
                 }

             }

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {   // Now this is where I am testing to see what actual value is stored in my    //value array by simply making it show up when I click the button.

        MessageBox.Show(val[0]);
    }
}

}

arrays are 0 indexed, that means that an array with 3 elements will have elements at indexes 0 , 1 , and 2 .

3 is out of bounds, so when you try to access pos[3] or len[3] , your program will thrown an exception.

use j < 3 instead of j<=3

 for (int j = 0; j < 3; j++)
 {
     val[j] = line.Substring(pos[j], len[j]); // THIS IS WHERE PROBLEM OCCURS
 }

The problem is that you go all the way up to j == 3 in the for statement. This will be the fourth element since arrays are zero-based, so change the for statement to:

for (int j = 0; j < 3; j++)

and you will be good to go.

The array pos has three values in it.

Consider your for loop.

  1. First i is zero. That is less than or equal to three.
  2. Then i is one. That is less than or equal to three.
  3. Then i is two. That is less than or equal to three.
  4. Then i is three. That is less than or equal to three.
  5. Then i is four. That is not less than or equal to three.

It executes the body of the loop 4 times. There are 3 items.

For the fix, to follow standard conventions, just use a less then, rather than a less than or equal to, in the condition check of your for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM