Index and Listbox error

Asked

Viewed 102 times

2

This is my code:

 public static ListBox listBox1 = new ListBox();
 Form2.Globals.listBox1.Items.Add(Form2.Globals.din);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.dequi);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.demer);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debai);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.deval);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.denin);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.degoo);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.dehub);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debqui);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debmer);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debbai);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debval);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debnin);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debgoo);
 Form2.Globals.listBox1.Items.Add(Form2.Globals.debhub);

 StreamWriter save = new StreamWriter(savegame);
 {
      for (int i = 0; i < Form2.Globals.listBox1.Items.Count; i++)
      {
            save.WriteLine(Form2.Globals.listBox1.Items[i].ToString());
      }
      save.Dispose();
      save.Close();
 }

 var din2 = Globals.listBox1.Items[0]; // <- line error
 Globals.din = Convert.ToDouble(din2);

Error:

An unhandled Exception of type 'System.Argued tofrangeexception' occurred in System.Windows.Forms.dll

Additional information: Invalidargument=Value '0' is not a value valid for 'index'.

Printscreen/Screenshot.

  • 1

    This code is very complicated and does things that should not, but the problem is that the Form2 before Globals.listBox1.Items on the error line. It’s just what I see differently that could cause the error.

1 answer

1


Note that throughout the code is using a list called Form2.Globals.listBox1, therefore this variable listBox1 is in Form2.Globals. But the moment you go to access the variable, the name is not complete and apparently there is a variable with the same main name exists in another place, and then this variable has no elements in it. On the error line is accessing Globals.listBox1. Is different.

The code has several other problems that seem to work, but is wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.