Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Got
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
as the index reaches 128. How can it be resolved? Thanks.

What I have tried:

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Posted
Updated 19-May-23 3:55am

1 solution

You can resolve it by not requesting items which are outside the bounds of the array.

Remember, the valid indexes for an array range from 0 to array.Length - 1. If you request an item outside of those bounds, you will get this exception.
 
Share this answer
 
Comments
s yu 19-May-23 10:17am    
Richard: Thanks for your post. The piece of code is
for (int j = 0; j < r.Length - 1; j++) { // the Length=145
ar[k+j, 0] = r[j][0]; // out of bound at j=128
}
Any idea? Thanks again.
Richard Deeming 19-May-23 10:19am    
So either k + j is less than 0 or greater than ar.GetLength(0) - 1; or r[j].Length is equal to 0.

You need to debug your code to find out which.
s yu 19-May-23 10:35am    
You are RIGHT! Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900