2010-05-07 219 views
11

我是一個非VB6的人誰不幸繼承了一個錯誤的傳統VB6 /經典ASP項目。有一個部分將很多條目放入Dictionary,我希望看到它包含的所有條目。我想這(oParams是一個字典):迭代通過VB6字典

Dim o As Object 
Dim sDicTempAggr As String 
sDicTempAggr = "" 
For Each o In oParams 
    sDicTempAggr = sDicTempAggr & ", " & o 
Next 

其中返回:

對象不支持此屬性或方法:438

使用Option Explicit,我怎麼遍歷通過VB6 Dictionary找出它包含的所有內容?

回答

12

下面是迭代的樣本,如果你還是看看問題在第二循環來檢查類型的值的字典中的

Dim oParams As New Dictionary 
    oParams.Add 1, "This" 
    oParams.Add 2, "That" 
    oParams.Add 3, "The other" 
    Dim key As Variant 
    Dim sDicTempAggr As String 
    Dim sTypes As String 
    For Each key In oParams.Keys 
     sDicTempAggr = sDicTempAggr & IIf(sDicTempAggr <> "", ",", "") & oParams(key) 
    Next key 
    For Each key In oParams.Keys 
      sTypes = sTypes & IIf(sTypes <> "", ",", "") & TypeName(oParams(key)) 
    Next key 
9

在字典的枚舉是Object類型不,你應該使用Variant代替:

Dim o As Variant 
Dim sDicTempAggr As String 
sDicTempAggr = "" 

For Each o In oParams 
    sDicTempAggr = sDicTempAggr & ", " & oParams(o) 
Next 

另外,For Each重點內沒有返回的字典成員因此oParams(o)的值,如果你更改爲對於Each o In oParams.items,您可以直接使用oParams