PC Review


Reply
Thread Tools Rate Thread

How to assign a collection item?

 
 
kingskippus@gmail.com
Guest
Posts: n/a
 
      27th Oct 2006
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sk1C?=
Guest
Posts: n/a
 
      27th Oct 2006
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


"(E-Mail Removed)" wrote:

> 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
>
>

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      28th Oct 2006

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



<(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
key/value collection that allows key string to be updated and retains collection item entry order dx Microsoft Dot NET Framework 2 25th Sep 2004 05:51 PM
Item Collection Editor doesn't preserve collection Andrés Giraldo Microsoft ASP .NET 2 25th Mar 2004 08:38 PM
Custom collection: how to find item in collection using custom indexer panik Microsoft C# .NET 0 21st Aug 2003 09:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:45 PM.