Private oField As Collection = New CollectionI add items in a collection like this:
' Add a field in the collection ' expC1 Field Private Function AddField(ByVal tcField As String) As Boolean Dim lnKey As Integer = 0 Dim loObject(3) As Object ' Initialization loObject(1) = tcField loObject(2) = True lnKey = oField.Count + 1 loObject(3) = lnKey oField.Add(loObject, lnKey) Return True End FunctionThen, in the code, based on some conditions, I may need to update an item in the collection. As we know, we cannot do that but to remove and add the item back in the collection. So, I usually do it like this:
' Get the list of fields missing from the dataset For Each loObject In oField ' Initialization lcField = loObject(1) lnKey = loObject(3) ' Logic here to determining if we have found it llFound = ... ' If we found it If llFound Then ' Remove this one from the collection oField.Remove(lnKey) ' Add the updated item in the collection loObject(2) = False oField.Add(loObject, lnKey) End If NextWhile this has worked so far on pretty much everywhere I have used it, I discovered today that in a particular situation, it doesn't work. The problem relates to when adding the item back in the collection. On some of them, I get "Add failed. Duplicate key value supplied.".