我对这件事有多难感到惊讶,但我想这是一个快速修复,所以我会在这里询问(搜索谷歌和文档,但都没有帮助)。我有一些代码可以使用键将项目添加到集合中。当我遇到集合中已经存在的键时,我只想通过在当前值上添加一个数字来设置它。
代码如下:
If CollectionItemExists(aKey, aColl) Then 'If key already has a value
'add value to existing item
aColl(aKey).Item = aColl(aKey) + someValue
Else
'add a new item to the collection (aka a new key/value pair)
mwTable_ISO_DA.Add someValue, aKey
End If
第一次将键/值对添加到集合中时,我添加一个整数作为值。当我再次遇到该键时,我尝试向该值添加另一个整数,但这不起作用。我认为问题不在于任何类型的对象不匹配或类似的情况。我当前收到的错误消息是
运行时错误 424:需要对象
I'm surprised at how hard this has been to do but I imagine it's a quick fix so I will ask here (searched google and documentation but neither helped). I have some code that adds items to a collection using keys. When I come across a key that already exists in the collection, I simply want to set it by adding a number to the current value.
Here is the code:
If CollectionItemExists(aKey, aColl) Then 'If key already has a value
'add value to existing item
aColl(aKey).Item = aColl(aKey) + someValue
Else
'add a new item to the collection (aka a new key/value pair)
mwTable_ISO_DA.Add someValue, aKey
End If
The first time I add the key/value pair into the collection, I am adding an integer as the value. When I come across the key again, I try to add another integer to the value, but this doesn't work. I don't think the problem lies in any kind of object mis-match or something similar. The error message I currently get is
Runtime Error 424: Object Required
发布评论
评论(3)
一旦将值添加到集合中,您就无法对其进行编辑。所以这是不可能的:
相反,您可以从集合中取出对象,编辑其值,然后将其添加回来。
这有点乏味,但是将这三行放在 Sub 中就可以了。
当集合用作对象的容器时(与整数、双精度等“原始”变量的容器相反),它们会更加友好。您无法更改集合中包含的对象引用,但可以操作附加到该引用的对象。
顺便说一句,我认为您误解了与
Item
相关的语法。你不能说:aColl(aKey).Item
。正确的语法是aColl.Item(aKey)
,或者简称为aColl(aKey)
,因为Item
是 Collection 的默认方法目的。然而,我更喜欢使用完整、明确的形式......You can't edit values once they've been added to a collection. So this is not possible:
Instead, you can take the object out of the collection, edit its value, and add it back.
This is a bit tedious, but place these three lines in a Sub and you're all set.
Collections are more friendly when they are used as containers for objects (as opposed to containers for "primitive" variables like integer, double, etc.). You can't change the object reference contained in the collection, but you can manipulate the object attached to that reference.
On a side note, I think you've misunderstood the syntax related to
Item
. You can't say:aColl(aKey).Item
. The right syntax isaColl.Item(aKey)
, or, for short,aColl(aKey)
sinceItem
is the default method of the Collection object. However, I prefer to use the full, explicit form...字典
比集合
更通用且更省时。如果您选择了这条路线,您可以直接在下面的字典上运行一个简单的Exists
测试,然后更新键值Patrick Matthews 在 词典 v 集合
Dictionaries
are more versatile and more time efficient thanCollections
. If you went this route you could run an simpleExists
test on the Dictionary directly below, and then update the key valuePatrick Matthews has written an excellent article on dictionaries v collections
我认为您需要删除现有的键值对,然后再次将键添加到集合中,但使用新值
I think you need to remove the existing key-value pair and then add the key to the collection again but with the new value