Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Maintaining collection
Message
From
22/05/2013 11:59:42
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Maintaining collection
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01574508
Message ID:
01574508
Views:
63
I would like to verify something on maintaining a collection.

The collection is defined like this:
    Private oField As Collection = New Collection
I 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 Function
Then, 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

        Next
While 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.".

We all know that the collection handling is a problem in .NET. The reason is because the second parameter, when adding an item in the collection, is known as the key. But, when adding or removing, we have to use an index. That approach has worked so far. It doesn't work today.

Anyone would have a better idea on updating an item in the collection?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform