-3

When running through a loop, my loop processes through the first iteration without errors, then fails with an Index out of Bounds error. Walking through the code, I'm not able to see why it's throwing this exception.

I also changed the loop iterations to i < 1, which allowed the program to run. When I changed the iterations to i < 2, I received the exception error again.

The code I have does this:

    private void build()
    {
      AllPartsList.PartsList.Clear();
      AllPartsList.PartsList.Add(new InHouse(1, "Part 1", 5.0, 5, 15, 3, 25));
      AllPartsList.PartsList.Add(new InHouse(2, "Part 2", 10.0, 10, 25, 5, 2));
      AllPartsList.PartsList.Add(new Outsourced(3, "Part 3", 15.0, 12, 20, 7, "Acme"));
      AllPartsList.PartsList.Add(new Outsourced(4, "Wheel", 12.0, 15, 30, 10, "Carpathia"));
      AllPartsList.PartsList.Add(new Outsourced(5, "Pedal", 8.0, 24, 50, 22, "BendORama"));
      AllPartsList.PartsList.Add(new Outsourced(6, "Chain", 9.0, 12, 15, 3, "Michael's Metals"));
      AllPartsList.PartsList.Add(new InHouse(7, "Seat", 4.0, 8, 10, 2, 15));
    }

    private void display()
    {
      PartTable.Rows.Clear();
      PartTable.Refresh();
      for (int i = 0; i < AllPartsList.PartsList.Count; i++)
      {
        PartTable.Rows[i].Cells[0].Value = AllPartsList.PartsList[i].PartID;
        PartTable.Rows[i].Cells[1].Value = AllPartsList.PartsList[i].PartName;
        PartTable.Rows[i].Cells[2].Value = AllPartsList.PartsList[i].price;
        PartTable.Rows[i].Cells[3].Value = AllPartsList.PartsList[i].inStock;
      }
    }

AllPartsList class:

    class AllPartsList
    {
      private static BindingList<Part> partsList = new BindingList<Part>();
      public static BindingList<Part> PartsList { get { return partsList; } set { partsList = value; } }

      public static string CurrentPart { get; set; }
      public static int CurrentPartID { get; set; }
      public static int CurrentPartIndex { get; set; }
      public static double CurrentPartPrice { get; set; }
      public static int CurrentPartInventory { get; set; }

      public static Part lookupPart(int i)
      {
        for (int j = 0; j < PartsList.Count; j++)
        {
          if (PartsList[j].PartID.Equals(i))
          {
            CurrentPartIndex = j;
            return PartsList[j];
          }
        }
        CurrentPartIndex = -1;
        return null;
      }

      internal static void swap(Part prt)
      {
        PartsList.Insert(CurrentPartIndex, prt);
        PartsList.RemoveAt(CurrentPartIndex + 1);
      }
    }  

From what is programmed, I would expect the loop to run through 7 iterations, then load the information to the DataGridView when I run the program.

mcalex
  • 6,628
  • 5
  • 50
  • 80
Jason Rapp
  • 187
  • 1
  • 10
  • did you check if your PartTable contains enough Rows to cater your AllPartsList iteration? – SKLTFZ Apr 25 '19 at 03:22
  • Can't you use your `AllPartsList.PartsList` as the DataSource of the DGV? – Jimi Apr 25 '19 at 03:27
  • When you examined `PartTable.Rows` and `AllPartsList.PartsList` in the debugger what did you find? – HABO Apr 25 '19 at 03:32
  • @SKLTFZ: PartTable is the DGV. – Jason Rapp Apr 25 '19 at 03:34
  • @NatPongjardenlarp: I don't think this is the same... I know WHAT the error is, I just don't understand why I'm getting it, as nothing in the code indicates that I should be getting it. – Jason Rapp Apr 25 '19 at 03:37
  • @Jimi: Added the `AllPartsList.PartsList` class to the question. – Jason Rapp Apr 25 '19 at 03:41
  • @JasonRapp try to remove 4 lines of code about PartTable within your loop and see if the error persisted. if there is no error, then it means the issue is about your DGV instead of the iteration of AllPartsList – SKLTFZ Apr 25 '19 at 03:46
  • 2
    This is a complete lack of debugging, you haven't even worked out which index is out of range, and just leaving us to guess. Step through with the debugger and give us more information – TheGeneral Apr 25 '19 at 03:51
  • @TheGeneral: I did step through with the debugger. The value that comes as out of bounds is when `i = 1`, and is out of bounds on the left side of the assignment operator for the first statement in the loop: `PartTable.Rows[i].Cells[0].Value = AllPartsList.PartsList[i].PartID;`. As I said, it works for the first iteration, then fails with the out of bounds error. – Jason Rapp Apr 25 '19 at 03:56
  • This is not what i asked, there are 3 indexers there, which one is it, which one is causing the exception, is it `Rows[i]` is it `Cells[0]` or is it `PartsList[i]` – TheGeneral Apr 25 '19 at 03:58
  • Did you miss adding new DGV row, before you allocating the data to columns. you might consider creating the new DGV row inside for loop. – Srikanth Apr 25 '19 at 04:42
  • You are calling `PartTable.Rows.Clear();` and then in the loop, trying to access `PartTable.Rows[i]`. Since there are no rows in the DGV (because you just cleared them), it throws the exception. Perhaps just before the `for` loop, you should call `PartTable.Rows.Add(AllPartsList.PartsList.Count);` to make sure there are enough rows in the table. – Chris Dunaway Apr 25 '19 at 16:38

1 Answers1

1

Chris Dunaway and Srikanth were correct: I wasn't adding any rows, because I had been staring at the code for hours at that point, and was just not thinking. Thanks for the help!

Jason Rapp
  • 187
  • 1
  • 10