0

I am tying to got the length of the 2nd dimension of the array using GetLength().

I read using GetLength(0) is the first dimension and using GetLength(1) is the second dimension.

Source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/a7ddde89-35cc-4a29-9aab-ac42b8fd75fe/c-30-two-dimensional-array-length?forum=csharpgeneral

However this is not working for me and is throwing: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' when trying to access the second dimension (with the second for loop).

Below is the code snippet I am using.

List<string> barDates = new List<string>();

        List<string> barValues = new List<string>();

        for (int i = 0; i < data.GetLength(0); i++)
        {
            MessageBox.Show(data[i][0] + " " + data[i][1]);

            for (int x = 2; x < data.GetLength(1) - 2; x++)
            {
                int value = 0; 

                value = value + Int32.Parse(data[i][x]);

                barValues.Add(value.ToString());
            }

            barDates.Add(data[i][1]);
        }

        barValues.ToArray();

        barDates.ToArray();

Here is the variable data with the values: https://i.stack.imgur.com/4t8Vp.png

The main goal of this is to read a .csv file containing 14 columns, the first one is the business name and the second is the year which are not needed inside the second for loop.

Please ignore MessageBox.Show(data[i][0] + " " + data[i][1]); as I have this just to see if the right values were being processed.

The number of rows is ever-changing so need to be dynamic.

All numbers from dimension 2 will be added together then added to an array for each row.

The array then will be used within a bar chart.

Connor Nee
  • 33
  • 6

1 Answers1

4
data[i][1]

This is a jagged array. data.GetLength(1) will not work, since data doesn't have a 2nd dimension. Use this instead:

data[i].Length
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44