0

I currently have a itemsCollection and a historyCollection. Both are separate isolated .dat files.

The saving and displaying works fine for both of these isolated storages, however the problem is when I try to delete something out of the itemCollection I also want to remove all the items out of the historyItemCollection where the historyItemCollection contains that specific itemIndex.

Update:

The itemCollection could contain something like this:

  • a
  • b
  • c

The historyItemCollection could look like this:

  • a
  • b
  • c
  • b

So if I remove b (itemIndex 1) then I want both to be removed in the historyItemCollection.

I can remove the items out of the itemCollection fine, but the historyItemCollection throws errors.

        int i = 0;
        for (i = 0; i <= App.ViewModel.historyItemCollection.Count-1; i++)
        {
            if (App.ViewModel.historyItemCollection[i].VehicleId == (Application.Current as App).vehicleIndex)
            {
                App.ViewModel.historyItemCollection[i].Remove(i);
            }
        }

        App.ViewModel.vehicleItemsCollection[(Application.Current as App).vehicleIndex].Remove((Application.Current as App).vehicleIndex);

The error message I'm getting: Parameter name: index

It fails on this line:

App.ViewModel.historyItemCollection[i].Remove(i);
webdad3
  • 8,893
  • 30
  • 121
  • 223
  • @Matt - I updated the entire question. As I'm looping through my collection (in my example), I'm getting the error on the last remove. The 1st remove worked fine... – webdad3 Mar 28 '11 at 14:09

2 Answers2

1

In this line do you really mean both of these to be itemIndex?

App.ViewModel.historyItemCollection[(Application.Current as App).itemIndex].Remove((Application.Current as App).itemIndex);

Also, does calling RefreshItems() have any impact on itemIndex?

I'd recommend breaking your code up into more lines - then single stepping through.


As an aside, why are you setting the DataContext in this method? Then why are you also setting it twice - and to different types of objects each time. This seems unusual - perhaps you could instead set the DataContext globally to a new ViewModel class which has the history and items as children?

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • I took out the RefreshItems and the DataContext as it doesn't do anything regarding the deletion. That was just left over code. – webdad3 Mar 28 '11 at 13:14
  • still looks to me like the two itemIndex values in the same line look wrong. – Stuart Mar 28 '11 at 14:01
  • now you have two vehicleIndex items on the same line... are you sure one of them shouldn't be an id? – Stuart Mar 28 '11 at 14:11
  • If you break the code out into single line operations and then step through them then you will be able to tell what is null - e.g. put `var temp = App.ViewModel.vehicleItemsCollection[(Application.Current as App).vehicleIndex];` on a separate line to `temp.Remove((Application.Current as App).vehicleIndex);` - and better yet pull `(Application.Current as App)` out into a local variable too. – Stuart Mar 28 '11 at 14:13
  • @Stuart - the thing is the remove for the vehicleItemsCollection is working... I'm not as concerned about that one as I am the historyItemCollection. – webdad3 Mar 28 '11 at 14:15
  • Two answers - 1. You might be doing the for loop equivalent of "cutting down the tree while you are climbing it" - try `for (i = App.ViewModel.historyItemCollection.Count-1; i >= 0 ; i--)` – Stuart Mar 28 '11 at 14:20
  • 2. I don't know what you are using for collections, but are you sure you want `Remove` and not `RemoveAt` – Stuart Mar 28 '11 at 14:20
  • And again, I encourage you to step through the code with a debugger and a watch window - ideally write a test - you'll soon spot what is going wrong. – Stuart Mar 28 '11 at 14:21
  • @Stuart - I was thinking that maybe going up through the loop might have an effect with the later items as well. I'll try that (as well as your other suggestions). – webdad3 Mar 28 '11 at 14:24
  • @Stuart - Thanks for this. Your help and advice helped me fix the issue. I believe looping through it from the bottom up was the issue. – webdad3 Mar 29 '11 at 13:11
1

The problem is that you're removing items from a list as you iterate forwards through it.
As you delete items from the list you invalidate the count of total items in the list and your relative position.

If you must edit the list this way you should step backwards through it so as not to effect the indexing.

See also How to remove elements from a generic list while iterating over it?

Community
  • 1
  • 1
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143