Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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# ArgumentOutOfRangeException未处理_C#_Exception_Xna - Fatal编程技术网

C# ArgumentOutOfRangeException未处理

C# ArgumentOutOfRangeException未处理,c#,exception,xna,C#,Exception,Xna,异常的进一步详细信息:索引超出范围。必须为非负数且小于集合的大小。参数名称:索引 当阅读异常时,我理解它试图告诉我什么。我不明白的是——为什么——它就要来了。以下是相关代码的片段: //the model contains more than one mesh, so each //one must be accounted for in the final sphere List<BoundingSphere> spheres = n

异常的进一步详细信息:索引超出范围。必须为非负数且小于集合的大小。参数名称:索引

当阅读异常时,我理解它试图告诉我什么。我不明白的是——为什么——它就要来了。以下是相关代码的片段:

        //the model contains more than one mesh, so each
        //one must be accounted for in the final sphere
        List<BoundingSphere> spheres = new List<BoundingSphere>();
        int index = 0;

        //cycle through the meshes
        foreach (ModelMesh mesh in this.model.Meshes)
        {
            //and grab its bounding sphere
            spheres[index++] = mesh.BoundingSphere; //<- this is the line that throws the exception
        } //end foreach
//模型包含多个网格,因此每个网格
//必须在最后一个领域考虑一个人
列表范围=新列表();
int指数=0;
//在网格中循环
foreach(此.model.mesh中的ModelMesh网格)
{
//并抓住它的边界球
球体[index++]=mesh.BoundingSphere;//您需要使用list.Add(…)而不是索引

deafult的列表大小为0,您可以添加项目,但您对不存在的项目进行编码索引。即使索引为0,它也会失败。

您需要使用列表。添加(…)而不是索引


deafult将列表的大小设置为0,您可以添加项,但您可以对不存在的项进行编码索引。即使索引为0,它也会失败。

您需要使用add方法来增加列表的大小。因此,请尝试
spheres.add(mesh.BoundingSphere);
而不是
spheres[index++]=mesh.BoundingSphere;

您需要使用Add方法来增加列表的大小。因此,请尝试
spheres.Add(mesh.BoundingSphere);
而不是
spheres[index++]=mesh.BoundingSphere;
您想要编写
spheres.Add(mesh.BoundingSphere)
。您的
球体
列表在创建后为空。您无法访问不存在的项目。

您的意思是编写
球体。添加(mesh.BoundingSphere)
。您的
spheres
列表在创建后为空。您无法访问不存在的项。

您可以将其编写为
var-spheres=model.mesh.Select(m=>m.BoundingSphere.ToList();
var-spheres=(从模型中的m.mesh选择m.BoundingSphere.ToList())
您可以将其写成
var-spheres=model.mesh.Select(m=>m.BoundingSphere.ToList();
var-spheres=(从模型中的m.mesh选择m.BoundingSphere.ToList();