Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Maintaining collection
Message
From
22/05/2013 12:16:53
 
 
To
22/05/2013 11:59:42
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
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:
01574510
Views:
38
Likes (1)
>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?

1) As far as I know, Collection is a VB specific class. Dictionary< TKey, TValue> would be more language agnostic, and likely have better support for LINQ. It should also give you O(1) access to the element you are looking for if you know the key.
2) Your object should be a reference type and you should be able to update it directly without removing it from the collection. The example at http://msdn.microsoft.com/en-us/library/vstudio/cy13e6ex%28v=vs.100%29.aspx under Accessing Elements confirms it.
3) Lock the collection when you add and remove elements to prevent other threads from doing the same.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform