How to assign a collection item?

K

kingskippus

Maybe I'm just being dense, but I can't figure out how to do this.
Here's a code snippet, copied directly from a test function:

'----------------------------------------
Dim foo As New Collection
foo.Add 10, "bar"
MsgBox "value: " & foo("bar") ' Displays value: 10 in a message box
foo("bar") = 20 ' This gives a Run-time error '424': Object required
'----------------------------------------

If I try Set foo("bar") = 20, I get the same thing. If I try something
like this, I get the same thing, again, even if I try Set foo("bar") =
i:

'----------------------------------------
Dim foo As New Collection
Dim i as Integer: i = 10
foo.Add i, "bar"
MsgBox "value: " & foo("bar")
i = 20
foo("bar") = i ' This gives a Run-time error '424': Object required
'----------------------------------------

I also tried it without the key ("bar") and using a numeric index
instead with the same result. I also tried it using the
fully-qualified foo.Item("bar") and foo.Item(1) with the same result,
always a Run-time error '424'! So my question is, how do I assign an
element of a collection after it's been added? Do I really have to
remove it and re-add it, because that seems kind of silly. Or am I
just overlooking something really simple? (Definitely within the realm
of the possible...)

Or on the other hand, am I just using a Collection for something it's
not intended to be used for? I've got a dynamic set of variables that
I need to store information in and that change. Is there a better
structure to use or method of implementing something like this?

Thanks,
-KS
 
J

JMB

If you set a reference to the Microsoft Scripting Runtime (Tools/References),
maybe you could use a dictionary object

Sub test()
Dim dTemp As Scripting.Dictionary

Set dTemp = New Dictionary
dTemp.Add "a", 1
dTemp.Add "b", 2

MsgBox dTemp("a")
dTemp("a") = 20
MsgBox dTemp("a")

End Sub
 
J

Jim Cone

The Dictionary object is better at this then a Collection,
however...

Sub ColTest()
Dim foo As Collection
Set foo = New Collection
foo.Add 10, "bar"
foo.Add 22, "bar1"
foo.Remove ("bar")
foo.Add 20, "bar", before:="bar1"

MsgBox "value: " & foo("bar")
Set foo = Nothing
End Sub
-----------
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



<[email protected]>
wrote in message
Maybe I'm just being dense, but I can't figure out how to do this.
Here's a code snippet, copied directly from a test function:
'----------------------------------------
Dim foo As New Collection
foo.Add 10, "bar"
MsgBox "value: " & foo("bar") ' Displays value: 10 in a message box
foo("bar") = 20 ' This gives a Run-time error '424': Object required
'----------------------------------------
If I try Set foo("bar") = 20, I get the same thing. If I try something
like this, I get the same thing, again, even if I try Set foo("bar") =i
'----------------------------------------
Dim foo As New Collection
Dim i as Integer: i = 10
foo.Add i, "bar"
MsgBox "value: " & foo("bar")
i = 20
foo("bar") = i ' This gives a Run-time error '424': Object required
'----------------------------------------
I also tried it without the key ("bar") and using a numeric index
instead with the same result. I also tried it using the
fully-qualified foo.Item("bar") and foo.Item(1) with the same result,
always a Run-time error '424'! So my question is, how do I assign an
element of a collection after it's been added? Do I really have to
remove it and re-add it, because that seems kind of silly. Or am I
just overlooking something really simple? (Definitely within the realm
of the possible...)

Or on the other hand, am I just using a Collection for something it's
not intended to be used for? I've got a dynamic set of variables that
I need to store information in and that change. Is there a better
structure to use or method of implementing something like this?
Thanks,
-KS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top