I have the following two for loops in sequence. It’s very strange that the first for loop works when I run it alone. But when I run the second loop alone, I get a index out of range exception error. Can someone help me to check what the error is? Thanks a lot!
for (i = NiPricePointer; i < 551; i++)
{
tempUpper = tempUpper + NiPriceCounter[i];
if (tempUpper >= (NiPriceRounds * 0.3))
{
chart3.Series["Upper 30%"].Points.AddXY(k * 500, ((i - 1) * 0.1 + 5));
break;
}
}
for (i = NiPricePointer; i>0; i--) //This loop always gives me out of range problems.
{
tempLower = tempLower + NiPriceCounter[i];
if (tempLower >= (NiPriceRounds * 0.3))
{
chart3.Series["Lower 30%"].Points.AddXY(k * 500, ((i - 1) * 0.1 + 5));
break;
}
}
The initialization of the array:
int[] NiPriceCounter = new int[551];
Thanks a lot!
I suppose, NiPricePointer simply is greater than 550. Both of your loops should contain both bounds as a check:
and