3

I'm using VBScript and I am wondering if is there any way to call a function name stored in string variable?

Here is my attempt?

a = "b"

sub b() 
    msgbox "c"
end sub

a()

But it always result in an error

Type mismatch 'a'

0

3 Answers 3

18

The correct answer is: Use GetRef() as in:

Function F(p)
  F = p + p
End Function

Dim FP : Set FP = GetRef("F")

WScript.Echo FP("a")
WScript.Echo FP(123)

Output:

aa
246
Sign up to request clarification or add additional context in comments.

1 Comment

This is a better answer and should be the accepted one. This method also works in ASP Classic.
4

Seems like e.g. this should work:

Dim a
a = "Call b()"
Eval(a)

Sub b
   ' Do stuff
End Sub

1 Comment

@user433531 Please look at the answer of Ekkehard.Horner, GetRef is the right (and secure!) way to do it. Eval is not secure, uses global scope, is slow and you cannot easily pass arguments in your function. And a function pointer created by GetRef can be passed around as an object, which is a huge pro.
-3
Dim x

Sub b

 print "xxx"' Do stuff
End Sub

x = "call b()"

Execute(eval("x"))

2 Comments

There is no print in VBScript; the task is to call a function via its name.
Could you please elaborate more your answer adding a little more description about the solution you provide?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.