-2

The hangman game I'm trying to make throws "ArgumentOutOfRangeException" at the for loop when I'm trying to display the underlines. I tried rewriting the code but nothing worked.

            List<Label> underline;
            int left = 300;
            int top = 275;

            for (int i = 0; i < data.solution.Length; i++) //data is the object that contains the word that is the solution
            {
                underline = new List<Label>();
                underline[i].Parent = this;
                underline[i].Text = "_";
                underline[i].Size = new Size(35, 35);
                underline[i].Location = new Point(left, top);
                left += 30;
                underline[i].Font = new Font(new FontFamily("Microsoft Sans Serif"), 20, FontStyle.Bold);
            }

I don't understand what is wrong with this. It throws it right away when I click on new game.

lapartman
  • 45
  • 1
  • 7
  • A [minimal complete and verifiable example](https://stackoverflow.com/help/mcve) would really help. –  May 10 '18 at 06:59
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – ProgrammingLlama May 10 '18 at 07:14

2 Answers2

4

You instantiate a new list of labels on every for loop iteration here:

for (int i = 0; i < data.solution.Length; i++)
{
     underline = new List<Label>();
     ...
}

What you probably wanted to achieve is to create a list of labels and modify them. For that you could create a list and then create labels and add them to this list one by one:

var underline = new List<Label>(data.solution.Length);
for (int i = 0; i < data.solution.Length; i++)
{
     var lbl = new Label();
     lbl.Parent = ...
     ...
     underline.Add(lbl);
}

Another solution would be to use LINQ:

var underline  = data.solution.Select(x = 
                      {
                          left += 30;
                          return new Label 
                          { 
                             Parent = this,
                             Text = "_", 
                             ...
                          }
                      }).ToList();
3

First don't create a new list for every iteration in the for loop, create it outside of the loop. Second you have to add a new Label instance to the list before you can access it trough it's index:

List<Label> underline = new List<Label>();
int left = 300;
int top = 275;

for (int i = 0; i < data.solution.Length; i++) //data is the object that contains the word that is the solution
{
    underline.Add(new Label());
    underline[i].Parent = this;
    underline[i].Text = "_";
    underline[i].Size = new Size(35, 35);
    underline[i].Location = new Point(left, top);
    left += 30;
    underline[i].Font = new Font(new FontFamily("Microsoft Sans Serif"), 20, FontStyle.Bold);
}
Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33