无法在每个循环中正确遍历VB6中的键AND值

时间:2019-07-17 00:15:21

标签: foreach vb6

我正在VB6中编写程序,但无法正确遍历String字典。

我尝试了两种方法来访问集合中的值。 Collection(Key)Collection.Item(Key)

Dim line As Variant
Dim thismsg As New Collection
Dim thissection As String
For Each line In Split(NetRcv, vbLf)
    If Left(line, 3) = "BLK" Then
        thissection = Right(line, Len(line) - 3)
        MsgBox thissection
        GoTo nctlrParseLoopNext
    End If
    If Left(line, 3) = "BND" Then
        Exit For
    End If
    Dim key, value As String
    key = Left(line, InStr(line, " "))
    value = Right(line, InStr(line, " "))
    thismsg.Add key, value
nctlrParseLoopNext:
Next line
Dim member As Variant
For Each member In thismsg
    MsgBox member
    MsgBox thismsg(member)
Next member

NetRcv中的字符串如下:

BLK modeswitch
mode codeslave
BND

我希望看到MsgBoxes的顺序...

modeswitch
mode
codeslave

...在某处可能带有尾随空格。 我看到前两个,然后出现错误

Run-time error '5':
Invalid procedure call or argument

我不明白为什么会发生此错误。

member 键,对吗?

如果是,则没有理由应弹出此错误。

1 个答案:

答案 0 :(得分:2)

一方面,您已反转了值和键。这个:

thismsg.Add key, value

应该是这样:

thismsg.Add value, key

有关Add方法的doco,请参见此处

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/add-method-visual-basic-for-applications