Share via


Visual Basic Troubleshooting: .Net Common Errors: "Index was out of range."

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

The error message couldn't do a better job in conveying what went wrong. Lets back up...

An index is an integer that identifies the location of an element in a array, list, or other collection type. When accessing items in an array, list or other collection type, you use parenthesis with an index number inside. Array(Index# or Integer Variable).

What is the range?

A range is comprised of two parts:

  1. Inclusive Lower Bound
    • The Lower Bound is the lowest available index number in the collection, and should always be zero. There will not be a zero index in the collection if there are no items in the collection.
  2. Inclusive Upper Bound
    • The Upper Bound is the highest available index number in the collection.

What do you mean by Inclusive?

This means that this number is included in the range.

Example:

  • Lets say your lowerbound is zero(it always is)
  • Lets say your upperbound is 10
  • This means there would be 11 Items total located at the following indexes: 0,1,2,3,4,5,6,7,8,9,10

Why does the 'Index was out of range.' error occur?

Remember, this error message does an excellent job of conveying what went wrong and what to avoid! It tells you:

  • The index must be non-negative, which means the lowest possible index number that can exist is zero.
  • The index number that you are using must be smaller than the size of the collection. 

Example

  • If the collection contains a total of 6 elements, since the first index in the collection will be zero, instead of 1, this means that you will never have an index of six(otherwise there would be 7 elements in the collection). Therefore the UpperBound of the collection is calculated like this UpperBound = Collection.Items.Count - 1, or UpperBound = UBound(Array)

That's it, just make sure that you are calculating your indexes correctly.

How to avoid this error:

  • Always check what the UpperBound(highest available index) of that particular collection is.
  • Only attempt to access that index once you have confirmed that it exists

Example

Option Strict On
Public Class  Form1
 Private Sub  Button1_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button1.Click
 Dim MyArray As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 Dim Tmp As Integer
 For I = 0 To UBound(MyArray)
 'This will not go outside of the bounds of the array
 'no error will be caught here
 Try
 Tmp = MyArray(I)
 Catch ex As Exception
 MsgBox(ex.Message)
 End Try
 Next
 For I = 0 To MyArray.Count - 1
 'This will not go outside of the bounds of the array
 'no error will be caught here
 Try
 Tmp = MyArray(I)
 Catch ex As Exception
 MsgBox(ex.Message)
 End Try
 Next
 For I = 0 To 9
 'This will not go outside of the bounds of the array
 'no error will be caught here
 Try
 Tmp = MyArray(I)
 Catch ex As Exception
 MsgBox(ex.Message)
 End Try
 Next
 For I = 0 To MyArray.Count - 1
 'This will not go outside of the bounds of the array
 'no error will be caught here
 Try
 Tmp = MyArray(I)
 Catch ex As Exception
 MsgBox(ex.Message)
 End Try
 Next
 For I = 0 To MyArray.Count
 'This will go outside of the bounds of the array
 'and an error will be caught
 Try
 Tmp = MyArray(I)
 Catch ex As Exception
 MsgBox(ex.Message)
 End Try
 Next
 For I = 0 To 10
 'This will go outside of the bounds of the array
 'and an error will be caught
 Try
 Tmp = MyArray(I)
 Catch ex As Exception
 MsgBox(ex.Message)
 End Try
 Next
 End Sub
End Class

References

See also

Please check out my other Technet Wiki articles!