0

I defined an array player using struct igralec that accepts one string and one integer. I need array player in other functions too because i have to manipulate with the data inside.

However, if I try to run the whole code I get an error in a last for loop at line

player[i].Name = myList[i];

already at first index i.

The error says: An unhandled exception of type "System.IndexOutOfRangeException" occured in game.exe.

igralec[] player { get; set; }

public struct igralec
    {
        public string Name;
        public int Points;
        public igralec( string ime, int tocke)
        {
            Name = ime;
            Points = tocke;
        }
    }

    public void PlayButton_Click(object sender, EventArgs e)
        {
            List<string> myList = new List<string>();
            //declaring a list of strings to which all the player names are copied
            for (int i = 0; i < PlayersList.Items.Count; i++)
            {
                PlayersList.Items[i].Selected = true; //marking the items from the list as selected
            }
            foreach (ListViewItem Item in PlayersList.SelectedItems)
            {
                myList.Add(Item.Text.ToString()); //add to list all selected items
            }
            if (PlayersList.Items.Count != 0 && SteviloTock.SelectedItem != null) {
                //collect the data in struct vector so I can use it outside this function
                player = new igralec[] { };
                //rewrite the array so you can use it outside the function
                for (int i = 0; i < myList.Count; i++)
                {
                    player[i].Name = myList[i];
                    player[i].Points = StTock;
                }
            }
        }

What is the problem here?

skrat
  • 648
  • 2
  • 10
  • 27

2 Answers2

1

You have created a new array player = new igralec[] { }; then immediately try to access the first element in the array i = 0, player[i].Name = myList[i];

player[0] doesn't exist yet (empty array) so the System.IndexOutOfRangeException is thrown. You need to add a new element to the player collection instead.

You could consider using a List if you don't know the array size when creating the collection, as suggested in Adding values to a C# array:

List<igralec> igralecList = new List<igralec>();
for (int i = 0; i < myList.Count; i++)
{
    igralecList.Add(new igralec(myList[i], StTock));
}

// You can convert it back to an array if you would like to
igralec[] igralecArray = igralecList.ToArray();

Or with a lambda expression (as suggested by Andrei Olariu):

player = myList.Select(i => new igralec(i, StTock)).ToArray();
Community
  • 1
  • 1
Alex
  • 115
  • 1
  • 8
1

Like others have said, it's because your array doesn't have a size.

You could also try this:

player = myList.Select(i => new igralec(i, StTock)).ToArray();

Instead of this:

//collect the data in struct vector so I can use it outside this function
player = new igralec[] { };
//rewrite the array so you can use it outside the function
for (int i = 0; i < myList.Count; i++)
{
    player[i].Name = myList[i];
    player[i].Points = StTock;
}
Andrei Olariu
  • 556
  • 4
  • 8