c# - System.IndexOutOfRangeException when trying to copy data from list to 2d array -
i getting exception:
system.indexoutofrangeexception: 'index outside bounds of array.'
on col ++ when in second loop. trying put vales list> excel columns , rows. please let me know doing wrong.
object[,] cellvaluestowrite = new string[excelrange.rows.count, excelrange.columns.count]; foreach (list<string> errorrecord in section.errorrecords) { int rows = 0; int col = 0; int length = errorrecord.count; foreach (var elem in errorrecord) { if (col < length) { cellvaluestowrite[rows, col] = elem; col++; } } rows++; }
if create array excelrange.rows.count
, excelrange.rows.count
5 cannot access cellvaluestowrite[5, col]
because indexing starts @ 0 , try access 6-th element way.
why not using old loop (especially if need indexing):
for (int row = 0; row < cellvaluestowrite.getlength(0); row++) { (int col = 0; col < cellvaluestowrite.getlength(1); col++) { cellvaluestowrite[row, col] = section.errorrecords[row][col]; } }
Comments
Post a Comment