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?