Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding and removing items in collection
Message
 
À
26/10/2015 13:38:21
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01626445
Message ID:
01626450
Vues:
62
This message has been marked as the solution to the initial question of the thread.
>I have a collection. I add them using a unique identifier. However, before adding a new one, I verify something. If that is the case, I remove the related item from the collection, by using its key, update something in its content, and insert the item back into the collection by using the same key.
>
>So, it goes like this:
>
>
>' Get the item from the collection
>loObject = oInnerJoin.Item(lnKey)
>
>' Remove the item from the collection
>oInnerJoin.Remove(lnKey)
>
>' Set the search criteria to True
>loObject(1) = True
>
>' Add the updated item in the collection
>oInnerJoin.Add(loObject, lnKey, 1)
>
>
>So, in a specific logic, this is executed three times on the same key. The first two times, it works. I see the collection item being removed and added back with the object loObject(1) item. However, on the third time, it gives this:
>
>"Add failed. Duplicate key value supplied."
>
>Anyone would be able to explain that?

I cannot explain it, I cannot even understand the code, is loObject an array? (you have loObject(1) = True)

Anyways, if you are storing an object as the name of the variable suggests, you should not need to update the collection, what is stored in the collection is the pointer to the object, so when you modify it the collection should have the new value, for example in FoxPro (Should be the same in VB or any other OO language):
lcKey			= 'myLine'
loInnerJoin		= CREATEOBJECT('Collection')
loObject		= CREATEOBJECT('Line')
loObject.Left	= -11

loInnerJoin.Add(loObject, lcKey)				&& Add the object to the collection

loObject		= loInnerJoin.Item(lcKey)		&& Get the object from the collection
? 'Initial value:', loObject.Left	&& -11 as it was the value I assigned first

loObject.Left	= 22				&& Change the value of the property

loObject		= loInnerJoin.Item(lcKey)		&& Get the object from the collection again
? 'Updated value', loObject.Left	&& -22 as the object is the one that has been changed
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform