如何更改集合项目的价值

编程入门 行业动态 更新时间:2024-10-19 03:25:46
本文介绍了如何更改集合项目的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用此代码(在excel-vba中),我根据数组将许多项添加到集合中。 我将数组的值用作键,并将字符串 NULL用作值

With this code (in excel-vba) I add to a collection a number of items depending on an array. I use the value of the array as key and the string "NULL" as value for each item added.

Dim Coll As New collection Dim myArr() Set Coll = New collection myArr() = Array("String1", "String2", "String3") For i = LBound(myArr) To UBound(myArr) Coll.Add "NULL", myArr(i) Next i

现在,如果我想更改项目的值,通过键对其进行标识,我必须删除该项目,然后添加具有相同密钥的项目,还是可以更改项目的值?

Now, if I want to change the value of an item, identifying it by the key, I must remove the item and then add an item with same key or is it possible to change the item value?

这是唯一的方法吗?

Coll.Remove "String1" Coll.Add "myString", "String1"

还是有类似的东西:(我知道那不起作用)

Or is there something like: (I know that doesn't work)

Coll("String1") = "myString"

推荐答案

Yo您还可以编写一个(公共)函数来对集合进行更新。

You can also write a (public) function to make updates to a collection.

public function updateCollectionWithStringValue(coll ax Collection, key as string, value as string) as collection coll.remove key coll.add value, key set updateCollectionWithStringValue = coll end function

您可以通过以下方式调用此函数:

You can invoke this function by:

set coll = updateCollectionWithStringValue(coll, "String1","myString")

然后,您需要调用一个班轮。

Then you have a one liner to invoke.

更多推荐

如何更改集合项目的价值

本文发布于:2023-11-27 21:54:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   价值   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!