IndexOutOfRangeException on multidimensional array despite using GetLength check

I am using the following code to read values from an Excel spreadsheet:

// Start with passed int lastPassRow = sheets[passedVehicles].GetLength(0); for (int i = 1; i < lastPassRow; i++) { // Exception here if(DateTime.TryParse(sheets[passedVehicles][i, 0].ToString(), out result)) { passedDates.Add((DateTime)sheets[passedVehicles][i, 0]); } }

The type of sheets[passedVehicles] is a multidimensional array of Object[,] and the for loop above is giving me an IndexOutOfRange exception at i = 1, despite that I check the number of rows.

I added some logging for the spreadsheet in question, and have verified:

    <li>i = 1 is the iteration that is failing</li> <li>The value of lastPassRow is 4</li> <li>The value of sheets[passedVehicles].GetLength(1) is also four.</li> </ul>

    All values appear to be in range to me. Is there something else that could cause this exception?

    Note: I am starting at i = 1 because row 0 is a header in the spreadsheet and does not contain data I am trying to read.


    I suspect it's the 0 that's out of range.

    Excel arrays are 1-based, not 0-based as you might expect.


  • WTForms : How to add “autofocus” attribute to a StringField
  • Convert String to Double
  • TimeSpan Conversion
  • How to prevent Internet Explorer Connection Timeout?
  • How can I find the longest string in a multi-dimensional array?
  • PHP Database driven Multilevel Menu
  • Parsing decimal with thousands separator
  • What determines the order validators fire in?
  • How to get the relative position of a Dictionary element?
  • Extract zip entries to another Zip file
  • ActiveRecord query for a count of new users by day
  • Illegal mix of collations for operation for date/time comparison
  • Volley JsonObjectRequest send headers in GET Request
  • Date difference with leap year
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • javascript inside java/jsp code
  • Javascript convert timezone issue
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Android Studio and gradle
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?