2011-04-18 75 views
31

在向VBA集合添加一些值之后,有没有办法保留所有密钥的列表?VBA集合:密钥列表

例如

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" 

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

first string 
second string 
third string 

:有什么办法留住钥匙?

first key 
second key 
third key 

注:我使用VBA通过AutoCAD 2007的

回答

26

我不认为可能与香草集合,没有一个独立的存储阵列中的键值。最简单的替代方法是添加对Microsoft脚本运行时的引用&而不是使用更强大的字典;

Dim coll As New Dictionary 
... 
Dim K As Variant 
For Each K In coll.Keys 
    debug.print "Key: " & K , "Value: " & coll.Item(K) 
Next 
+2

它可以在每台计算机上100%工作吗? – 2011-04-18 13:33:55

+1

我想对Windows的所有最新版本都说它是Windows脚本的一部分,但我看不到明确的答案。 – 2011-04-18 13:43:15

+7

只是为了澄清,这种方法应该适用于所有的Windows操作系统,但它不适用于Mac OS。 – 2015-04-22 15:56:25

32

如果您打算使用默认VB6 Collection,那么你可以做最简单的就是:

col1.add array("first key", "first string"), "first key" 
col1.add array("second key", "second string"), "second key" 
col1.add array("third key", "third string"), "third key" 

然后你可以列出所有值:

Dim i As Variant 

For Each i In col1 
    Debug.Print i(1) 
Next 

个或所有密钥:

Dim i As Variant 

For Each i In col1 
    Debug.Print i(0) 
Next 
+1

我之前使用此解决方案。我正在寻找更多_pretty_。但是,谢谢,无论如何:) – 2011-04-18 13:34:56

+1

不错的解决方案,对我来说似乎相当漂亮,字典可能更强大,但是这种方法没有Windows脚本的外部依赖性,即在Mac OS上也不需要额外的维护或其他类 – nkatsar 2017-06-18 16:09:53

+2

当使用这可能更喜欢使用** VBA.Array **,即'col1.add VBA.Array(“first key”,“first string”),“first key”',以避免由“Option Base 1" 。这样,结果数组的下限总是** 0 ** – nkatsar 2017-06-18 16:31:16

8

另一种解决方案是将密钥存储在单独的收集:

'Initialise these somewhere. 
Dim Keys As Collection, Values As Collection 

'Add types for K and V as necessary. 
Sub Add(K, V) 
Keys.Add K 
Values.Add V, K 
End Sub 

您可以维护的键和值,这可能是有用的,有时单独的排序顺序。

+0

在Alex K.给我介绍字典之前,我有时会使用类似的算法。现在我使用字典,它更好:)但谢谢你。 – 2012-04-25 07:59:55

8

您可以创建一个小类来保存键和值,然后将该类的对象存储在集合中。

类KEYVALUE:

Public key As String 
Public value As String 
Public Sub Init(k As String, v As String) 
    key = k 
    value = v 
End Sub 

然后使用它:

Public Sub Test() 
    Dim col As Collection, kv As KeyValue 
    Set col = New Collection 
    Store col, "first key", "first string" 
    Store col, "second key", "second string" 
    Store col, "third key", "third string" 
    For Each kv In col 
     Debug.Print kv.key, kv.value 
    Next kv 
End Sub 

Private Sub Store(col As Collection, k As String, v As String) 
    If (Contains(col, k)) Then 
     Set kv = col(k) 
     kv.value = v 
    Else 
     Set kv = New KeyValue 
     kv.Init k, v 
     col.Add kv, k 
    End If 
End Sub 

Private Function Contains(col As Collection, key As String) As Boolean 
    On Error GoTo NotFound 
    Dim itm As Object 
    Set itm = col(key) 
    Contains = True 
MyExit: 
    Exit Function 
NotFound: 
    Contains = False 
    Resume MyExit 
End Function 

这是当然类似于字典建议,除没有任何外部依赖性。如果你想存储更多的信息,这个类可以根据需要变得更加复杂。

+0

在字典建议中使用这种方法有什么好处吗?你能否在这种情况下解释外部依赖的缺点? – JackOrangeLantern 2012-08-08 17:42:58

0

麻烦的是,对于大型数据集,集合执行方式比任何字典更好...

+0

这不回答问题 – WhatsThePoint 2017-05-26 13:46:32

+0

这不提供问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/16242516) – 2017-05-26 19:53:28

+1

这不会回答这个问题,但Dictionary对于Collection来说是很慢的选择。按照https://stackoverflow.com/questions/47019932/performance-alternative-over-scripting-dictionary。我目前只在需要调试代码时才使用Dictionary(Scripting或其他语言),然后将代码切换到Collections,一旦它足够稳定。 – Dexterial 2018-02-15 15:35:11