c# - Strange behavior from Excel.Worksheet.Cells[row, column] -


i have following method:

    public static object getvalue(excel.worksheet sheet, int row, int col)     {         excel.range r = sheet.cells[row, col] excel.range;         //i've tried using sheet.get_range(cell, cell); here, same result          if (r.row != row || r.column != col)          {                //why debug statement print?             debug.print("tried read (" + row.tostring() + ", " + col.tostring() + ") got (" + r.row.tostring() + ", " + r.column.tostring() + ")");         }          return r.value2;     } 

based on understanding of excel.worksheet.cells[row, column], code should never enter if statement. however, when call getvalue repeatedly read multiple cells, every row , column of range returns different row , column called excel.worksheet.cells with.

example output: "tried read (19, 1) got (56, 5)"

furthermore, if break in if statement, rewind execution point , run excel.worksheet.cells again, correct range.

why be?

this isn't answer direct question, may solve problem.

is getvalue in tight loop? if approach different way. since accessing cells , value2 com-calls slower normal array accessors. if have particular range you're looping on array of values range in memory , access array directly rather looping through cells.

e.g. instead of

for(int r = 1; r <= 10; r++)     for(int c = 1; c <= 20; c++)             double val = sheet.cells[r,c]; 

do

object[,] values = sheet.range("a1:j20").value for(int r = 1; r <= 10; r++)     for(int c = 1; c <= 20; c++)             double val = values[r,c]; 

this may fix problem removing com access tight loop , eliminating whatever problem giving strange results.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -