解决visual basic 6.0的不匹配错误

时间:2011-12-17 13:18:59

标签: arrays vb6

当我运行此代码时,它会在第For i = 0 To UBound(arffArray)行显示一个对话框,显示“不匹配类型”。我的代码有什么问题?

Public Function processFile()

    Dim i, j, posRelation, temp, att, data, flag
    Dim strRelation
    Dim strAtt
    Dim strData

    strRelation = "@relation"
    strAtt = "@attribute"
    strData = "@data"

    att = 0
    data = 0

    For i = 0 To UBound(arffArray)
        If (InStr(arffArray(i), strRelation)) Then
            temp = Replace(Mid(arffArray(i), 11, Len(arffArray(i))), "'", "")
            RelationName = temp
        ElseIf (InStr(arffArray(i), strAtt)) Then
            flag = parseAtt(att, arffArray(i))
            If (Not flag) Then
                processFile = flag
                Exit Function
            End If
            att = att + 1
        ElseIf (InStr(arffArray(i), strData)) Then
            data = readTheRest(i)
            i = UBound(arffArray) 'end the loop
            totalData = data
        End If
    Next

    'get the list of class name
    Dim tmpClassAttr
    tmpClassAttr = attArray(1, UBound(attArray, 2))

    For i = 0 To UBound(tmpClassAttr)
        ReDim Preserve classArray(i)
        classArray(i) = Trim(tmpClassAttr(i))
    Next

    processFile = True
End Function

'------------------------------------------------------------
'Function: parseAtt(num, attrData, ByVal m As MineKnow)
'require:
' >@num      -> current attribute counter
' >@attrData -> current attribute declaration
'Raises: error if reading non numeric data/attribute
'Return: boolean parseAtt TRUE/FALSE, TRUE if parse successfully or otherwise
'Effect: parsing file content to:
' > attributes
'------------------------------------------------------------
Private Function parseAtt(num, attrData)
    Dim temp, i, j, strAtt, temp2, pos, atVal
    ReDim Preserve attArray(2, num)

    'possible type of declarations
    '@attribute outlook {sunny, rainy, overcast}
    '@attribute outlook {sunny,rainy,overcast}
    'attribute pos = 12

    'get the attribute name first get the pos of "{"
    pos = InStr(1, attrData, "{", 1)

    If (pos = 0) Then
        error = "---->Nominal attribute only." & vbCrLf & "---->" & attrData
        parseAtt = False
        Exit Function
    Else
        strAtt = Trim(Mid(attrData, 12, pos - 12))
        atVal = Mid(attrData, pos + 1, Len(attrData) - (pos + 1))

        atVal = Replace(atVal, "'", "")
        atVal = Replace(atVal, "''", "")
        atVal = Replace(atVal, "}", "")
        atVal = Replace(atVal, " ", "")

        temp = Split(atVal, ",")

        attArray(0, num) = strAtt
        attArray(1, num) = temp
        parseAtt = True
    End If
End Function

1 个答案:

答案 0 :(得分:4)

您的代码最大的问题是您没有声明变量的类型!!

之类的陈述
Dim i

只会将变量i声明为类型Variant,这绝对不是您想要的。

您确实希望将i声明为Integer。您可以通过在声明点明确指定类型来实现:

Dim i As Integer

由于For循环期望一个类型为Integer的迭代器索引变量,这应该可以阻止它阻塞该错误。

同样,字符串应始终显式声明为String类型:

Dim strRelation As String

请注意,当您在一行上声明多个变量时(出于清晰原因,您可能不应该这样做),您需要确保为每个变量指定类型。它不像其他语言那样累积。

例如,这句话

Dim i, j, k As Integer

仅将 k声明为Integerij类型为Variant,几乎可以肯定(再次)不是您想要的。

相反,你需要写

Dim i As Integer, j As Integer, k As Integer

当然,在这种特殊情况下,由于VB 6允许你使用下限不是0的变量,所以最好像这样对循环进行编码:

Dim i As Integer
For i = LBound(arffArray) To UBound(arffArray)
    ' Do something with the array
Next i