0

(This post has been edited with more information because it was to simplified)

The code below results in an System.IndexOutOfRangeException: Index was outside the bounds of the array

and I am trying to figure out why this happens.

The sample is a bit simplified.

[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;
        }
    }
}

trying to do this:

// 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

I have tried some different approaches, but everyone ends up with an exception.

What have I misunderstood?

Answer:

As Patric Hofman correctly pointed out, I was setting homeTeamName to an empty array with the size set to zero. In arrays you set the size starting at 1, but when adding items you start at position 0.

Just in case others stumbles over this as well, here is a very simple example to tell how arrays work:

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]);
    }
}

This example can also be found here:

https://dotnetfiddle.net/JpJyDg

Again, thanks for the very valuable feedback and aid from the Stack Overflow community.

Trond

5
  • 1
    can you show the class definition of bar? Commented Feb 5, 2016 at 10:53
  • For me this code compiles and runs just fine. Obviously you simplfied your code too much (except the thing that codeFoo[0] does not contain any member named bar). Commented Feb 5, 2016 at 10:54
  • Why not use public bar[] barfield {get;set} ? Commented Feb 5, 2016 at 10:55
  • 4
    I think you've simplified it a bit too much. What is bar supposed to be and why does it have another property bar (as with codeBar[0].bar)? Commented Feb 5, 2016 at 10:55
  • hopefully it is better now. Commented Feb 5, 2016 at 11:05

1 Answer 1

1

The problem is here:

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

You are not creating a new team, you are creating an empty array with the size set to 0. You can't set the first index (0) if the array is empty.

var homeTeamName = new name[1];

That would be a better option.

Sign up to request clarification or add additional context in comments.

1 Comment

so obvious I got blind looking at it for so many hours. Thanks Patrick for pointing that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.