-1

Maybe I'm just not seeing why, but I am getting an index out of range exception. I am adding items to an array(assignment requirement, I know I should be using a list or SQL server directly to store this information if this were a normal situation) through my addItem method, which takes an inventory item as an argument.

        public void addItem(Inventory item)
    {
        if (inventory.Length >= capacity)
        {
            capacity *= 2;
            Inventory[] temp = new Inventory[capacity];
            int i = 0;
            foreach (var inv in inventory)
            {
                temp[i] = inventory[i];
                i++;
            }

            inventory = temp;
        }

        inventory[size] = item;
        ++size;
        return;
    }

While trying to remove the item with my removeItem method, which also takes an inventory item as an argument, I'm getting an index out of range exception. I'm not seeing why, as when I remove the element from my inventory array, I should be shifting all elements over.

        public void removeItem(Inventory item)
    {
        int index = Array.IndexOf(inventory, item);
        for (int i = index; i < size; i++)
        {
            inventory[i] = inventory[i + 1];
        }
    }

I know it's just going to end up being an off by one error that I'm not seeing how to fix and kick myself for it.

edit: these are the initial values of size, capacity, and the array.

    Inventory[] inventory = new Inventory[10];
    int capacity = 10;
    int size = 0;

edit2: i and index are sitting at -1, which I'm unclear on.

int index = Array.IndexOf(inventory, item);

This should be grabbing the index of an inventory item from my array, however is returning -1 to me as if it does not exist. In my winform, I have each text field feeding into an item to be removed here:

private void btnRemoveItem_Click(object sender, EventArgs e)
    {
        int value;
        double filler;
        if (int.TryParse(textBoxSku.Text, out value) && int.TryParse(textBoxQuantity.Text, out value) && double.TryParse(textBoxPrice.Text, out filler))
        {
            //parsing successful
            Inventory item = new Inventory(int.Parse(textBoxSku.Text), int.Parse(textBoxQuantity.Text), textBoxDescription.Text, double.Parse(textBoxPrice.Text));
            app.removeItem(item);

}

bynary
  • 89
  • 7
  • 1
    Please re-read [mre] guidance on posting code for debugging questions. In particular it is not clear what are values of `size`, `capacity`, `i`, `index` and `inventory.Length` at the moment of the exception. Ideally you should replace all values with inline constants for the real sample (it is ok to keep overall code at the end of the question for the context). – Alexei Levenkov Apr 22 '22 at 17:12
  • Edited question to include their initial values. – bynary Apr 22 '22 at 17:15
  • I do understand what an indexOutOfBounds/range exception is, however in this context I do not understand why the thing that I am trying to do with removing one element from an array is sending me out of bounds. – bynary Apr 22 '22 at 17:20
  • How "Edited question to include their initial values. " is an answer to " values of ... **at the moment of the exception**" request for clarification? Please, please, please read [MRE] guidelines. – Alexei Levenkov Apr 22 '22 at 17:22
  • @derpirscher while obviously wrong code, I strongly doubt that it is the problem here... I expect `i` to be -1, hopefully OP will be able to [edit] the question with information requested... – Alexei Levenkov Apr 22 '22 at 17:25
  • @alexeiLevenkov i is sitting at -1, as suspected. – bynary Apr 22 '22 at 17:26
  • Btw `inventory.Length >= capacity` will always be true if `inventory = new Inventory[capacity]` so your array will double every time you add an element ... – derpirscher Apr 22 '22 at 17:27
  • And see for instance https://stackoverflow.com/questions/44992402/why-is-array-indexof-not-checking-for-iequatable-like-listt-does on searching with `Array.IndexOf` – derpirscher Apr 22 '22 at 17:35
  • Multiple bugs in removeItem(). 1: item not found => i = -1 is out of bounds. 2: remove last item => i+1 is out of bounds. Do note that this code re-invents the `List` class. – Hans Passant Apr 22 '22 at 17:44

1 Answers1

0

It looks to me like this line would fail...

inventory[i] = inventory[i + 1];

when 'i' is equal to 1 less than the length of your array, i + 1 will be outside the bounds of your array.

I suggest updating your for loop to run from index to 'i < size - 1'.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
RehtomRuoy
  • 19
  • 2
  • This doesn't seem to be the issue, as i/index are sitting at -1 to start, despite using the add/remove functions on the exact same data. I've updated my post to reflect the additional information I believe to be relevent – bynary Apr 22 '22 at 17:34
  • Whie indeed that code is wrong this does not answer the question at all. OP clearly stated that `i` is -1 (which was far more likely reason even before the eidt). – Alexei Levenkov Apr 22 '22 at 17:34