Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组中包含项时发生OutOfRangeException错误?_C#_Arrays_List_Xna - Fatal编程技术网

C# 数组中包含项时发生OutOfRangeException错误?

C# 数组中包含项时发生OutOfRangeException错误?,c#,arrays,list,xna,C#,Arrays,List,Xna,我有3门课,大致是这样的: 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();

我有3门课,大致是这样的:

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?
    }
}
为什么这会导致OutOfRange异常?错误告诉我它来自“cList”,即使在b类中,我添加了32项。

我认为这行

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.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类

Add(new cList{bunch of variables here})

看起来cList有1个元素cList。所以acess应该是这样的

var x=cList[0]

然后

itemAtIndex10=x[10]

编辑:我的坏,没有发现这是循环。我不会删除它,但它是正确的答案,抱歉://

尝试更改

public class b() {

因为您当前正在做的是在b类中创建第二个名为b的类,而不是为b创建构造函数

也就是说,我不知道你是怎么做到的

cList.Add(new cList { bunch of variables here });

甚至编译。

显然,当您尝试访问
[10]
时,没有
[10]
。你用调试器看了吗?@Hayden Perry你真的在构造函数中有关键字类吗?我在下面说那是一个打字错误,woops。。。我已经编辑过了。我确信每个人都在说的不是问题p此外,如果我知道我正在脱离RangeException而不是编译错误,那么这显然是一个输入错误?是的,但他引用了
[10]
,所以这没关系。那是一个输入错误。。。我现在已经修好了。我的代码又长又复杂,所以我想我应该用一种更简单的方式重新打印出来,但考虑到拼写错误,这显然是个坏主意:p OP清楚地说他添加了32项,如你所见。public class b(){也不可能编译所有的true。再看一遍-我怀疑在键入示例时,class关键字一定是意外添加的-所以我的建议不会有任何帮助。抱歉。cList.Add的另一个输入错误…我今天着火了:p