VBA 集合:键列表 [英] VBA collection: list of keys

查看:35
本文介绍了VBA 集合:键列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我向 VBA 集合中添加一些值后,有没有办法保留所有键的列表?

After I add some values to the VBA collection, is there any way to retain the list of all keys?

例如

Dim coll as new  Collection
Dim str1, str2, str3
str1="first string"
str2="second string"
str3="third string"
coll.add str1, "first key"
coll.add str2, "second key"
coll.add str3, "third key"

我知道如何保留字符串列表:

I know how to retain the list of strings:

first string
second string
third string

再说一遍:有没有办法保留密钥?

Once again: is there any way to retain the keys?

first key
second key
third key

注意:我通过 AutoCAD 2007 使用 VBA

推荐答案

我认为如果没有将键值存储在独立数组中的情况下使用普通集合是不可能的.

I don't thinks that possible with a vanilla collection without storing the key values in an independent array.

最简单的替代方法是添加对 Microsoft Scripting Runtime 的引用 &改用功能更强大的字典:

The easiest alternative to do this is to add a reference to the Microsoft Scripting Runtime & use a more capable Dictionary instead:

Dim dict As Dictionary
Set dict = New Dictionary

dict.Add "key1", "value1"
dict.Add "key2", "value2"

Dim key As Variant
For Each key In dict.Keys
    Debug.Print "Key: " & key, "Value: " & dict.Item(key)
Next

这篇关于VBA 集合:键列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆