这不是你如何在VBScript中将动态数组作为函数参数传递的吗?

时间:2017-01-20 10:03:17

标签: vbscript

好的,我正在尝试将给定十进制数的用户转换为二进制数,但是Internet Explorer给了我这个错误:

Type mismatch error

是不是你如何在VBScript中将动态数组作为函数参数发送?

VBScript代码:

Function toBinary(number, binary)
    Dim remainder : remainder = 0
    Dim index : index = 0

    While (number <> 0)
        remainder = number Mod 2
        number = number \ 2
        ReDim binary(index)
        binary(index) = remainder
        index = index + 1
    Wend

    toBinary = binary
End Function

Dim number
Dim response : response = vbYes
Dim binary()

While (response = vbYes)
    number = InputBox("Enter A Decimal Number: ")
    If (Not IsNumeric(number)) Then
        response = MsgBox("Wrong Input, Wanna Try Again? ", vbYesNo)
    Else
        MsgBox (number & " is equal to " & toBinary(number, binary) & " in Binary")
        response = vbNo
    End If
Wend

1 个答案:

答案 0 :(得分:1)

在行msgbox ( number & " is equal to " & toBinary(number, binary) & " in Binary")中,函数toBinary(...)返回一个数组。 数组无法转换为vbscript中的字符串,因此当您连接它以显示它时,会出现错误。

在您的示例中,我建议您构建一个字符串而不是数组并返回该字符串:

Function toBinary(value)
    dim result : result = ""
    dim remainder : remainder = 0

    If value = 0 Then
        result = "0"
    Else
        While (value <> 0)                               
            remainder = value Mod 2
            result = remainder & result
            value = value \ 2
        Wend  
    End If

    toBinary = result
End Function 

dim number
dim response : response = vbYes                  
dim binary() 

while ( response = vbYes )
    number = inputbox("Enter A Decimal Number: ")
    if ( Not IsNumeric(number)) then 
        response = msgbox("Wrong Input, Wanna Try Again ? ", vbYesNo)
    else
        msgbox ( number & " is equal to " & toBinary(number) & " in Binary")
        response = vbNo 
    end if      
wend