Reputation: 1347
For some reason, in this for loop, i is reaching 1, and causing an index out of range
error. Items.Count
is equal to 4, I checked that using a breakpoint, and StockList.Count
is also equal to 4. I can't seem to figure out why i is reaching one, any idea?
for (int i = 0; i <= (Items.Count / 4) - 1; i++)
{
for (int ii = 0;ii <= Program.StockList.Count - 1;i++)
{
if (Items[(i * 4) + 3] == Program.StockList[ii].ID) //Crash here
{
MessageBox.Show(Program.StockList[ii].Name + " Match!");
}
}
}
Upvotes: 0
Views: 93
Reputation: 35891
This (the second loop):
for (int ii = 0;ii <= Program.StockList.Count - 1;i++)
Should be this:
for (int ii = 0;ii <= Program.StockList.Count - 1;ii++)
I'm sure it's hard to spot the difference here, so no surprise it was even harder in your code. Consider using j
for the inner loop, and partitioning your code into smaller functions to avoid such mistakes.
Also as noted by kenny in the comments below, you can replace the second loop with a foreach
loop:
foreach (var stock in Program.StockList)
{
if (Items[(i * 4) + 3] == stock.ID)
{
//...
}
}
Upvotes: 5