Alamb
Alamb

Reputation: 137

What is the return type of my VBScript function?

This question is mostly out of interest to understand the functionality of VBScript better. I recognize that I can simply do some casting to know what to expect from my code, but in my situation I want to understand why casting, or any "workaround", is needed. For simplicity, here's the basic idea of my code:

variable1 = 1

Public Function findSomethingInATextString(par1, par2)
    [...searching with a Do Until loop code here...]
    Result = 1
    If([par2 is found in par1]) Then
        Result = 0
    End If
    Return Result
End Function

variable1 = findSomethingInATextString("Hello World", "Hello")

When I run this I get a Type Mismatch error. I don't understand why that's the case. variable1 is an integer and findSomethingInAString() returns an integer. They appear to be the same data type.

I'm working in a restricted environment where I can't do much debugging (it's painfully slow to code in this program...). So at the moment I'm unable to say what data type this is coming out as - I just know that it's apparently not integer.

After all that, and to make sure my question is clear, I'm intrigued to know what the return type of my function is (if someone happens to know), but my real question is: Why isn't the return type matching with variable1?

Upvotes: 0

Views: 3754

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Use the minimal script

Return

Output

cscript 36633603.vbs
...36633603.vbs(1, 1) Microsoft VBScript runtime error: Type mismatch: 'return'

to prove to yourself, that just mentioning return in a VBScript will throw a type mismatch error.

Believe JosefZ's comment that VBScript returns function values by assigning to the function's name. Better: Read the docs (before you try to write code).

Evidence:

Function f1()
  f1 = 1
End Function

WScript.Echo f1(), TypeName(f1())

Output:

cscript 36633603.vbs
1 Integer

Upvotes: 2

Related Questions