Accessing items added to a Collection

From "Visual Basic Concepts" in the MSDN

>Use the Add method to add an item to a collection. The syntax is:

>Sub Add (item As Variant [, key As Variant] [, before As Variant]

[, after As Variant] )

>For example, to add a work order object to a collection of work orders using the work order's ID property as the key, you can write:

>colWorkOrders.Add woNew, woNew.ID

>This assumes that the ID property is a String.

My question is:
If the work order's ID changes, how do i change the key in the collection so i can access the item as colWorkOrders.Item(woNew.NEWID).Name

P.S. I've considered the obvious of colWorkOrders.Remove(woNew.ID) and then
colWorkOrders.Add woNew, woNew.NEWID however i don't know how to copy the data to the new object.

Regards
Frantic
FranticAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James_stillmanCommented:
Sorry If I have misunderstood but why don't you remove the object. Change it's .ID property and then add it to the collection again.

as follows:

colWorkOrders.Remove(woNew.ID)

woNew.ID = woNew.NEWID

colWorkOrders.Add woNew, woNew.ID

Cheers
0
corvanderlindenCommented:
dim woNew as workorder

'
' get the original from the collection
'
set woNew = colWorkOrders.Item(oldID)

colWorkOrders.Remove oldID

colWorkOrders.Add woNew, woNew.NEWID


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James_stillmanCommented:
Oops Sorry I read the question wrong as I had someone talking in to me when I was reading it.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

FranticAuthor Commented:
I need another way so i can retain all the values set for this object.

Frantic
0
VBGuruCommented:
I think corvanderlinden idea is quite resonable.
0
FranticAuthor Commented:
corvanderlinden is correct in that the object will be removed and added with a new ID.

This is exactly NOT what i am trying to do.

If my object has other properties, i.e. Name, Age etc, i will lose these.

If i am understanding this incorrectly, please explain.

From my side all i am trying to do is change the Key of this object to match its new ID.

Regards
Frantic
0
AnswerTheManCommented:
<<If my object has other properties, i.e. Name, Age etc, i will lose these>>

no. you won't.

Assign the old object to a new temporary Object Variable.
then- remove it.
then change the ID prop in the temp object.
then - add the temp object to the collection.
then - set the temp object=nothing.

note that an object is killed ONLY when you set ALL (!!!) refrences  pointing at it to nothing. well, 'REMOVE' oes that if there is no other ref to the object. if there is (my suggestion) - the object is still alive, refrenced by the temporary Object variable, while 'REMOVE' just removes it from collection.

i hope you can see things better now.
0
BeauTCommented:
In corvanderlinden's comment, the object is still saved and then re-assigned. This line of code preserves the object in a separate variable:

set woNew = colWorkOrders.Item(oldID)

....and then the same object is reassigned with a different ID. By making this assignment, all property and internal data for the object in the collection is now stored in the woNew variable, even after the Workorder object is removed from the collection.  It can then safely be Added again with a new ID.

However, I also have another suggestion for you: Add a property to the workorder class called ID (or something similar) and change it's value when necessary, rather than going through the laborious process of re-assigning the object temporarily and then adding it again to the collection object.

So instead of this:

dim woNew as workorder

'
' get the original from the collection
'
set woNew = colWorkOrders.Item(oldID)

colWorkOrders.Remove oldID

colWorkOrders.Add woNew, woNew.NEWID

....you only have to do this:

'assign new ID
colWorkOrders(oldID).MyID = newID
0
AnswerTheManCommented:
Beaut : you are 9 minutes lagging...
0
FranticAuthor Commented:
I was hoping i could change the key without having to delete the object.

If this is not possible, i would have to go with corvanderlinden's answer.

Frantic
0
VBGuruCommented:
I think it is not possible,  i think in that case you got to implement your own collection to map to your requirement.
0
corvanderlindenCommented:
As an alternative you could use a dictionary object (VBScripting) instead of a collection. Then you can use the key property to change your key
0
FranticAuthor Commented:
How would this work?

I'm not so sure about not using collections.

0
corvanderlindenCommented:
A dictionary works almost as a collection, but you have more possibilities regarding accessing items and manipulating keys. You could say, it is a better implementation of a collection.
The disadvantage is you have no For Each...Next
Search for dictionary in the VB Help
0
Jeremy_DCommented:
> I was hoping i could change the key without having to delete the object

Actually, the way Cor proposes is exactly what you are looking for.
First of all, the difference between an object and a reference: an object is an actual instantiation of a class, while a reference is just a variable that POINTS TO an object. Now, an object will be destroyed when it has NO references pointing to it anymore.

In Cor's example, he makes a temporary variable and lets it reference (point to) the object which ID needs to be changed. After that he does the remove. Since there is still one reference pointing to the object (the temporary var just created), the object is NOT deleted, there is just one reference to it removed (the one in the collection), but there is still another reference, so the object is not destroyed. When you add it to the collection again, with the new key, you are creating another reference again (in the collection), but still to the same object, so it keeps all it's previous values. Changing the Key directly is not possible. The Collection object won't allow it (it would lose it's purpose if it did), and the object itself won't help you either, since the Key property is not a property from the object, but from the Collection class.

I wouldn't recommend using the dictionairy object. If you do, you'll have to distribute an extra dll (the dictionairy object comes from the Windows Scripting Host dll), and it doesn't make the job you need to do any easier than the example from Cor.

For some enlightment on the part of the dictionairy object, have a look at:
http://www.vb-zone.com/upload/free/features/vbpj/1999/mckinney/mckinney2b.asp#1
It's from the author of "Hardcore Visual Basic", THE bible for anyone who wants to make professional apps with VB.
0
FranticAuthor Commented:
Thank you Jeremy_D for your excellent explanation of how collections work.

I was not sure there was another way but after your explanation i can see why it would not work.

Technically corvanderlinden's answer was the right one but i new this when i posted the question.

Regards
Frantic
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.