【问题标题】:populating array in object in c#在c#中的对象中填充数组
【发布时间】:2016-02-05 10:49:46
【问题描述】:

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

下面的代码导致 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 社区提供的非常宝贵的反馈和帮助。

特伦德

【问题讨论】:

  • 你能显示bar的类定义吗?
  • 对我来说,这段代码编译并运行得很好。显然你把你的代码简化得太多了(除了codeFoo[0]不包含任何名为bar的成员)。
  • 为什么不使用public bar[] barfield {get;set}
  • 我认为你把它简化得太多了。 bar 应该是什么,为什么它有另一个属性 bar(与 codeBar[0].bar 一样)?
  • 希望现在更好。

标签: c# arrays exception


【解决方案1】:

问题出在这里:

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

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

var homeTeamName = new name[1];

那会是更好的选择。

【讨论】:

  • 太明显了,我看了好几个小时都瞎了。感谢帕特里克指出这一点。
猜你喜欢
  • 1970-01-01
  • 2018-11-14
  • 2022-01-24
  • 2016-01-19
  • 2022-08-02
  • 2020-02-23
  • 2021-08-16
  • 1970-01-01
相关资源
最近更新 更多