C # IndexOutOfRange problem, maybe simple

Because of this, hit her head against the wall. I get an error in the cell [rcell] = repack [counter], although I have 190 elements in the repack array.

        private string csvtogrid(string input)
    {
        input = input.Replace("\r", ",").Substring(2).TrimEnd(',').Trim().Replace("\n", ",").Replace(",,,", ",").Replace(",,",",");
        string[] repack = input.Split(',');
        string[] cell = { };
        int rcell = 1;
        for (int counter = 1; counter < repack.Length; counter++)
        {
            if (rcell < 4)
            {
                cell[rcell] = repack[counter];
                rcell++;
            }
            procgrid.Rows.Add(cell[1], cell[2], cell[3]);
            rcell = 1;
        }
        richTextBox1.Text = input;
        return null;
    }
+3
source share
1 answer

Your array is cellempty, so you cannot assign to an element cell[rcell]since it does not exist.

string[] cell = { };

You must specify a size that is large enough when you initialize it:

string[] cell = new string[4];
+2
source

Source: https://habr.com/ru/post/1745360/


All Articles