Error in index and ListBox

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.ArgumentOutOfRangeException'   occurred in System.Windows.Forms.dll

     

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

Printscreen / Screenshot .

    
asked by anonymous 07.12.2016 / 15:53

1 answer

1

Note that throughout the code you are using a list called Form2.Globals.listBox1 , so this variable listBox1 is in Form2.Globals . But at the moment it will access the variable, the name is not complete and apparently there is a variable with the same main name exists in another location, and there this variable has no elements in it. In the error line you are accessing Globals.listBox1 . It's different.

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

    
12.12.2016 / 12:11