Help With Collections

Nate Bross

Contributor
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
I have two collections of custom class objects like this.

Visual Basic:
Dim NewItems as Collection
Dim OldItems as Collection

Every user defined amount of time NewItems will be cleared and populated. I need to figure out if there is an item in NewItems that IS NOT in OldItems and if there is add it to a third collection.


I am trying to use the following code to check if the item is a re-occurance.
Visual Basic:
            If OldItems.Count <> 0 Then
                For Each OldItem As Item In OldItems
                    For Each NewItem As Item In NewItems
                        If NewItem.ItemID() IsNot OldItem.ItemID() Then
                            UnViewed.Add(NewItem)
                            Exit For

                        End If
                    Next
                Next
            Else
                OldItems = NewItems
            End If

The problem is that I get three hundred of the same entery into my UnViewed collection.

If anyone could help me with the logic of this, I'd really appreciate it.
 
Rather than using Collections you are probably better off with something like an ArrayList - you can then simply use the .IndexOf method to check to see if an item is in the ArrayList rather than looping through them yourself.

IIRC Collection is really nothing more than the VB6 collection object and as such doesn't have the same features of the newer collection / list classes.
 
Back
Top