我有三门课,它们或多或少都是这样的:
类a
class a {
b bObject;
public a() {
}
protected override void Draw(GameTime gameTime) {
bObject = new b(); // I know this is bad but I need this here
spriteBatch.Begin();
b.DrawFunction();
spriteBatch.End();
}
}类b
class b {
List<c> cList = new List<c>();
int itemAtIndex10;
public b() {
for(int i = 0; i < 32; i++) {
cList.Add(new c { bunch of variables here }); // 32 items in the list
}
}
public void DrawFunction() {
spriteBatch.Draw(drawstuff);
// more code
itemAtIndex10 = cList[10].variable; // This causes an OutOfRangeException?
}
}类c
class c {
// Loads of variables here
public c() {
}
}为什么这会导致OutOfRangeException?这个错误告诉我它来自"cList“,即使在b类中,我已经添加了32项。
发布于 2015-06-12 22:58:25
试着改变
public class b() {至
public b() {正如您当前所做的那样,在b类中创建第二个类b,而不是为b创建构造函数。
那就是说-我不知道你是怎么做到的
cList.Add(new cList { bunch of variables here });甚至编译。
发布于 2015-06-12 22:05:52
我觉得这句话..。
cList.Add(new cList { bunch of variables here }); // 32 items in the 应该是这条线。
cList.Add(new c { bunch of variables here }); // 32 items in the 除非我弄错了,cList是一个列表,其中c是下面的c类。
发布于 2015-06-12 22:30:22
CList.Add(新cList {此处变量束});
看起来cList有一个元素cList。所以acess应该像
var x= cList
然后
itemAtIndex10 = x10
编辑:我的坏,没有发现这是循环。我不会删除它,但答案是正确的,对不起:/
https://stackoverflow.com/questions/30811961
复制相似问题