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.
Matthew LuggPosted Aug 31, 2013, 7:25 AM
VulpesPosted Aug 31, 2013, 7:01 AM
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 LuggPosted Aug 31, 2013, 3:12 AM
VulpesPosted Aug 30, 2013, 1:46 PM
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 LuggPosted Aug 30, 2013, 12:58 PM
for (int i = 1; i == numbers.Length; i++) (is that right?)
but it still doesn't work.
VulpesPosted Aug 30, 2013, 12:29 PM
You sometimes get this with COM dlls.