简体   繁体   中英

Multi dimensional array out of bounds

My code is hanging on the line fi_get[1, 4] with an "index out of bounds" error.

The lines var is a string list array and at run time it had well over 600 lines of text in it. I don't know why I can't assign a string value to [1,4] ? It does assign values to [1,0] and [1,1] and [1,2] and [1,3] .

    string[,] fi_get = new string[2, 11];

     if (lines[i].Contains("show fabric-interconnect firmware"))
            {
                fi_get[0, 0] = Regex.Replace(lines[i + 2], @".+Running-Kern-Vers:\s(.+)", "$1");
                fi_get[0, 1] = Regex.Replace(lines[i + 3], @".+Running-Sys-Vers:\s(.+)", "$1");
                fi_get[0, 2] = Regex.Replace(lines[i + 4], @".+Startup-Kern-Vers:\s(.+)", "$1");
                fi_get[0, 3] = Regex.Replace(lines[i + 5], @".+Startup-Sys-Vers:\s(.+)", "$1");
                fi_get[1, 0] = Regex.Replace(lines[i + 7], @".+Running-Kern-Vers:\s(.+)", "$1");
                fi_get[1, 1] = Regex.Replace(lines[i + 8], @".+Running-Sys-Vers:\s(.+)", "$1");
                fi_get[1, 2] = Regex.Replace(lines[i + 9], @".+Startup-Kern-Vers:\s(.+)", "$1");
                fi_get[1, 3] = Regex.Replace(lines[i + 10], @".+Startup-Sys-Vers:\s(.+)", "$1");
            }

            if (lines[i].Contains("show fabric-interconnect inventory expand detail"))
            {
                fi_get[0, 4] = Regex.Replace(lines[i + 2], @".+Product Name:\s(.+)", "$1");
                fi_get[0, 5] = Regex.Replace(lines[i + 3], @".+PID:\s(.+)", "$1");
                fi_get[0, 6] = Regex.Replace(lines[i + 4], @".+Serial (SN):\s(.+)", "$1");
                fi_get[0, 7] = Regex.Replace(lines[i + 7], @".+Description:\s(.+)", "$1");
                fi_get[0, 8] = Regex.Replace(lines[i + 10], @".+Serial (SN):\s(.+)", "$1");
                fi_get[0, 9] = Regex.Replace(lines[i + 13], @".+Product Name:\s(.+)", "$1");
                fi_get[0, 10] = Regex.Replace(lines[i + 14], @".+PID:\s(.+)", "$1");
                fi_get[0, 11] = Regex.Replace(lines[i + 15], @".+Serial (SN):\s(.+)", "$1");
                fi_get[1, 4] = Regex.Replace(lines[i + 29], @".+Product Name:\s(.+)", "$1");
                fi_get[1, 5] = Regex.Replace(lines[i + 30], @".+PID:\s(.+)", "$1");
                fi_get[1, 6] = Regex.Replace(lines[i + 31], @".+Serial (SN):\s(.+)", "$1");
                fi_get[1, 7] = Regex.Replace(lines[i + 34], @".+Description:\s(.+)", "$1");
                fi_get[1, 8] = Regex.Replace(lines[i + 37], @".+Serial (SN):\s(.+)", "$1");
                fi_get[1, 9] = Regex.Replace(lines[i + 40], @".+Product Name:\s(.+)", "$1");
                fi_get[1, 10] = Regex.Replace(lines[i + 41], @".+PID:\s(.+)", "$1");
                fi_get[1, 11] = Regex.Replace(lines[i + 42], @".+Serial (SN):\s(.+)", "$1");
            }

An error I see is on fi_get[0, 11] = Regex.Replace(lines[i + 15] because second index should be between 0 and 10!!
Same error on fi_get[1, 11] = Regex.Replace(lines[i + 42] .

To answer your question

why I can't assign a string value to [1,4]?

Remember that C# arrays are zero based, so if you define fi_get[1, 4] you can assign values from fi_get[0, 0] to fi_get [0, 3] .
If you want to assign to fi_get[1, 4] you should define at least fi_get = new string[2, 5] .

I would bet that it's this expression:

lines[i + 29]

What is the value of i at the time this runs?

i + 29

is probably out of range. It's the only array index out of bounds exception that can be thrown on that line if this code is accurate.

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