(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
bar?codeFoo[0]does not contain any member namedbar).public bar[] barfield {get;set}?barsupposed to be and why does it have another propertybar(as withcodeBar[0].bar)?