Reference:
IndexOutOfRangeException Class (System) | Microsoft Docs[
^]
Quote:
The exception that is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds.
With yu logic of next/previous, you need to make sure at any given point of time, the value of
i
is always between 0-8 (as you shared there are 9 items).
My guess, if you use a DEBUGGER in your IDE, you will find that whenever the value is outside the range of 0-8 (either -1 or 9) it would throw the error.