【发布时间】:2012-04-22 14:32:50
【问题描述】:
谁能告诉我为什么会出现这个异常?好吧,我知道“为什么”我得到它是因为它说我尝试访问的列表中的元素不存在,但是单步执行代码我可以看到它确实存在?
在 Game1 中,我调用了辅助类的计算方法。然后当计算完成后,我将结果添加到 CalculationHelper 类中的 List 中。 代码如下:
在我实例化 CalculationHelper calcHelper 类的 Game1 类中,然后调用其中一个类方法。 首先,在 Game1 中,我在 for() 循环中调用 calcHelper.ForceVanishingPointIntersections(...) 方法,如下所示。这工作得很好, calcHelper.ForceVanishingPointIntersections(...) 方法将值添加到 CalculationHelper 类中的 IntersectionPointList (参见下面的方法)。
for (int i = 0; i < LineList.Count; i++)
{
calcHelper.ForceVanishingPointIntersections(LineList[i], LineList[i + 1]);
AddSprite(VanishingPointIntersectionList, calcHelper.GetIntersectionPointListElementAt(i), greenCirlce);
i++;
}
要做一些计算并向 CalculationHelper 类中的 IntersectionPointlist 添加一个值:
List<Vector2> IntersectionPointList = new List<Vector2>();
public void ForceVanishingPointIntersections(Line line1, Line line2)
{
Vector2 intersectionPoint;
// Do some calculations
IntersectionPointList.Add(intersectionPoint);
}
最后在 Game1 类中,for() 循环中的第二个方法我调用 AddSprite 来创建一个 Sprite。我想将存储在 CalculationHelper 类 IntersectionPointList 中的值作为 Sprite 的坐标传递。 为此,我调用 calcHelper.GetIntersectionPointListElementAt(i),它调用 CalculationHelper 类中的方法,如下所示(它应该从 IntersectionPointList 返回指定点 (i) 处的值) 公共 Vector2 GetIntersectionPointListElementAt(int index) { Vector2 结果 = this.IntersectionPointList[index]; 返回结果; } 第一次执行 for() 循环时,它工作正常。我进行计算,值存储在列表中,我能够从列表中获取该值并将其传递给 AddSprite(..)。但是,第二次循环 for() 时,当我从 AddSprite() 方法调用 GetIntersectionPointListElementAt 时,我的
中出现异常public Vector2 GetIntersectionPointListElementAt(int index)
{
Vector2 result = this.IntersectionPointList[index]; // Exception thrown here
return result;
}
说索引超出范围?但是单步执行代码,我的 IntersectionPointList 已更新,它显示该列表现在包含 2 个值。我正在尝试访问第二个值。
有人知道为什么会这样吗? 对于我的生活,我无法弄清楚我哪里出错了。
如果需要更多代码,我可以发布更多,请告诉我
【问题讨论】: