title: C# ArgumentOutOfRangeException when trying to retrieve data from a struct within an arraylist
body: <p>Will try to post only relevant code as my program is quite large already. Basically the program adds customer information into an arraylist-struct. I've got the storing and saving and file loading working flawlessly, but when I'm trying to show the data I'm getting exceptions. </p>
  
  <p>Most of the main code is on a class seperate to the forms, and this particular call comes from "frmViewRecords".</p>
  
  <pre><code>public void ViewData(int currentRecord)
      {
          string fn = ((custDetails)datalist[currentRecord]).firstName;
  
          frmViewRecords viewRecords = new frmViewRecords();
          viewRecords.WriteData(fn);
      }
  </code></pre>
  
  <p>The above code is what causes the exception, but the code for the messagebox below works fine.</p>
  
  <pre><code>public void LoadData()
      {
          bool fileLoaded = false;
  
          //Load the database
          try
          {
              FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); //Create the filestream
              try
              {
                  BinaryFormatter bf = new BinaryFormatter(); //New binary formatter for deserialization
                  datalist = (ArrayList)bf.Deserialize(fs);
                  fileLoaded = true; //Update the fileLoaded bool so that it doesn't open the file dialog instance
                  recordCount = datalist.Count;
                  MessageBox.Show("" + filename + " loaded successfully."); //Inform the user that the file was automatically loaded
                  MessageBox.Show("Test: " + ((custDetails)datalist[0]).firstName);
              }
              catch
              {
                  MessageBox.Show("Could not de-serialise from " + filename, "FILE LOADING PROBLEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
              }
              fs.Close();
          }
          catch
          {
              if (MessageBox.Show("File isn't in the right location, this is normal if a dataset does not yet exist.\n\n If the file exists elsewhere click no and you will be prompted to find the database file, else click yes to create a default file.", "FILE LOADING PROBLEM", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
              {
                  fileLoaded = true;
                  CreateDefaultData();
              }
          }
  </code></pre>
  
  <p>I have tried 'string fn = ((custDetails)datalist[0]).firstName;' to ensure it isn't the variable causing the problems, and the exception still happens. I am pretty much out of ideas. The problem cannot be with struct or arraylist definition as the the messagebox in LoadData() works fine and outputs the correct information. I tried moving the messagebox to the ViewData method and that also began giving an exception so I can only assume something is wrong with my method? </p>
  
  <p>These methods are on "MainClass.cs" and below is how I called the method from frmViewRecords:</p>
  
  <pre><code>MainClass mainClass = new MainClass();
  int currentRecord = 0;
  
  private void LoadData()
      {
          mainClass.ViewData(currentRecord);
      }
  </code></pre>
  
  <p>It might be worth mentioning that previously, I was calling the data straight from frmViewRecords like so:</p>
  
  <pre><code>txtFirstName.Text = ((MainClass.custDetails)mainClass.datalist[currentRecord].firstName;
  </code></pre>
  
  <p>But after getting the same exception while the messagebox prompt worked, I rewrote it to the above and still I get the problem so I have no idea what is causing it. </p>