通过VB6字典迭代

Din*_*nah 12 vb6 dictionary asp-classic

我是一个非VB6的人,不幸遗传了一个有缺陷的传统VB6/Classic ASP项目.有一个部分,其中有很多条目被放入Dictionary,我希望看到它包含的所有内容.我试过这个(oParams是一个字典):

Dim o As Object
Dim sDicTempAggr As String
sDicTempAggr = ""
For Each o In oParams
    sDicTempAggr = sDicTempAggr & ", " & o
Next
Run Code Online (Sandbox Code Playgroud)

返回的是:

Object不支持此属性或方法:438

使用Option Explicit,如何遍历VB6 Dictionary以找出它包含的所有内容?

cms*_*sjr 15

这是一个迭代的示例,如果你还有问题,请查看第二个循环来检查字典中值的类型

  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
Run Code Online (Sandbox Code Playgroud)


Ale*_* K. 10

字典上的枚举不是类型Object,您应该使用Variant:

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

For Each o In oParams
    sDicTempAggr = sDicTempAggr & ", " & oParams(o)
Next
Run Code Online (Sandbox Code Playgroud)

同样在For EachKey内部不返回字典成员因此oParams(o)为值,如果更改为For Each o In oParams.items,则可以oParams直接使用.