在c#中填充对象中的数组

时间:2016-02-05 10:49:47

标签: c# arrays exception

(这篇文章已经编辑了更多信息,因为它是简化的)

以下代码会产生一个 System.IndexOutOfRangeException:索引超出了数组的范围

我想弄清楚为什么会这样。

样本有点简化。

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://iptc.org/std/SportsML/2008-04-01/")]
[System.Xml.Serialization.XmlRootAttribute("team-metadata", Namespace = "http://iptc.org/std/SportsML/2008-04-01/", IsNullable = false)]
public partial class teammetadata
{

    private name[] nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("name")]
    public name[] name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://iptc.org/std/SportsML/2008-04-01/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://iptc.org/std/SportsML/2008-04-01/", IsNullable = false)]
public partial class name
{
    private string fullField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string full
    {
        get
        {
            return this.fullField;
        }
        set
        {
            this.fullField = value;
        }
    }
}

尝试这样做:

// Creating team meta data object
var teamMetaData = new teammetadata[1];

// creating home team meta data
var homeTeamMetaData = new teammetadata();

// Creating a new home team name
var homeTeamName = new name[0];

// Creating the team name
var teamName = new name { full = "Team name" };

homeTeamName[0] = teamName; // Ok

homeTeamMetaData.name = new name[] { teamName }; // Causes exception

homeTeamMetaData.name = homeTeamName; // Causes exception

我尝试了一些不同的方法,但每个人都有一个例外。

我误解了什么?

答案:

正如Patric Hofman正确指出的那样,我将homeTeamName设置为一个大小设置为零的空数组。在数组中,您将大小设置为从1开始,但在添加项目时,从位置0开始。

以防万一其他人偶然发现这一点,这里有一个非常简单的例子来说明数组是如何工作的:

using System;

public class Program
{
    public static void Main()
    {
        // Creating an array of strings which can hold up to two string objects
        var arrayString = new string[2];

        // Creating the first string object
        var stringItem1 = "Hello";

        // Adding the first string object to the array
        arrayString[0] = stringItem1;

        // Creating the second string object
        var stringItem2 = "World!";

        // Adding the second string object to the array
        arrayString[1] = stringItem2;

        // Write the output...
        Console.WriteLine(arrayString[0] + " " + arrayString[1]);
    }
}

这个例子也可以在这里找到:

https://dotnetfiddle.net/JpJyDg

再次感谢Stack Overflow社区提供的非常有价值的反馈和帮助。

特朗德

1 个答案:

答案 0 :(得分:1)

问题在于:

// Creating a new home team name
var homeTeamName = new name[0];

您没有创建新团队,而是创建一个大小设置为0的空数组。如果数组为空,则无法设置第一个索引(0)。

var homeTeamName = new name[1];

那将是一个更好的选择。