In a C# DLL, I have a for loop that retrieves data from an array named numbers. However, when the code to retrieve it runs, I get an IndexOutOfRangeException.
The exception says:
"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in ClassLibrary1.dll
Additional information: Index was outside the bounds of the array."
The code is:
for(int i = 0; i <= numbers.Length - 1; i++)
{
where.Text += numbers[i].ToString() + " /r/n ";
}
I apologise if it is something obvious - I am a beginner.
Many thanks.

VulpesRank #55 · 29.2K reputationPosted Aug 30, 2013, 8:29 AM EDT
You sometimes get this with COM dlls.
Matthew Lugg7 reputationPosted Aug 30, 2013, 8:58 AM EDT
for (int i = 1; i == numbers.Length; i++) (is that right?)
but it still doesn't work.
VulpesRank #55 · 29.2K reputationPosted Aug 30, 2013, 9:46 AM EDT
for (int i = 1; i <= numbers.Length; i++)
but I'd try printing out the values of the following to see whether they're what you'd expect them to be:
numbers.GetLowerBound(0)
numbers.GetUpperBound(0)
numbers.Length
Matthew Lugg7 reputationPosted Aug 30, 2013, 11:12 PM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 31, 2013, 3:01 AM EDT
So the next question is whether those values are constant.
I'd put a breakpoint on this line:
where.Text += numbers[i].ToString() + " /r/n ";
which is presumably the one which causes the exception and, by hovering your mouse over it, find out what the value of 'i' is then.
Matthew Lugg7 reputationPosted Aug 31, 2013, 3:25 AM EDT