user7374714
user7374714

Reputation:

Isn't this how you pass dynamic array as function argument in VBScript?

Okay, I am trying to convert the user given decimal number to binary but Internet Explorer gives me this error:

Type mismatch error

Isn't that how you send a dynamic array as function argument in VBScript?

VBScript code:

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

Upvotes: 0

Views: 246

Answers (1)

Foxtrot
Foxtrot

Reputation: 637

In the line msgbox ( number & " is equal to " & toBinary(number, binary) & " in Binary"), the function toBinary(...) returns an array. An array cannot be converted to a string in vbscript, so when you concatenate it to display it, you get an error.

In your example, I would suggest that you build a string instead of an array and return that string :

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

Upvotes: 1

Related Questions