Can anyone tell me why I am getting this exception? Well I know “why” I am getting it is cause it says that the element in the list I am trying to access does not exist, but stepping through the code I can see that it actually does exist?
In Game1 I call a helper class’ calculation method. Then when the calculation is done, I add the result to a List<> in the CalculationHelper class.
The code for this looks like:
In Game1 class where I have instanciated CalculationHelper calcHelper class and then call one of the class methods.
Fisrt, in Game1 I call calcHelper.ForceVanishingPointIntersections(…) method in a for() loop which can be seen below. This works just fine and the calcHelper.ForceVanishingPointIntersections(…) method adds the value to the IntersectionPointList<> in the CalculationHelper class (see method below).
for (int i = 0; i < LineList.Count; i++)
{
calcHelper.ForceVanishingPointIntersections(LineList[i], LineList[i + 1]);
AddSprite(VanishingPointIntersectionList, calcHelper.GetIntersectionPointListElementAt(i), greenCirlce);
i++;
}
To do some calculations and add a value to the IntersectionPointlist in CalculationHelper class I do:
List<Vector2> IntersectionPointList = new List<Vector2>();
public void ForceVanishingPointIntersections(Line line1, Line line2)
{
Vector2 intersectionPoint;
// Do some calculations
IntersectionPointList.Add(intersectionPoint);
}
Finally in Game1 class, the second method in the for() loop I call AddSprite to create a Sprite. I want to pass in the values stored in CalculationHelper class IntersectionPointList as coordinates for the Sprite.
To this end I call calcHelper.GetIntersectionPointListElementAt(i) which calls a method in CalculationHelper class like so (which should return the value at the specified point (i) from the IntersectionPointList<>)
public Vector2 GetIntersectionPointListElementAt(int index)
{
Vector2 result = this.IntersectionPointList[index];
return result;
}
The first time the for() loop executes, this works fine. I do the calculations, the value is stored in the list and I am able to get this value from the list and pass it to AddSprite(..). However, the second time round the for() loop, when I call GetIntersectionPointListElementAt from the AddSprite() method, I get an exception in my
public Vector2 GetIntersectionPointListElementAt(int index)
{
Vector2 result = this.IntersectionPointList[index]; // Exception thrown here
return result;
}
saying the index was out of range? But stepping through the code, my IntersectionPointList is updated and it shows that the list now contains 2 values. And I’m trying to access the second value.
Does anyone have an idea why this could be?
For the life of me I cant figure out where I am going wrong.
If more code is needed I can post some more, just let me know
Because you access
LineList[]with an indexi + 1you must diminish the last index by one in the for-condition. (Note the-1)This will call
ForceVanishingPointIntersectionswith the indexesNote that the index range of
LineListis0 ... Count-1.If non-overlapping indexes are required instead, like
then change the loop to
EDIT According the Chris Gessler’s comments this second approach is the right one.
Remove the
i++inside the loop, it’s quite uncommon and confusing. Instead, replace it byi += 2in loop header.Also note that (again according to Chris Gessler)
IntersectionPointListhas half as much items asLineList. Therefore callGetIntersectionPointListElementAtwithi / 2. Sinceiis{0, 2, 4, ... },i / 2is{0, 1, 2, ...}.The for-loop allows you to have a comma-separated list of statements in the first and third section. You could use it to maintain two indexes