2011-04-18 74 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