VBScript functions

Soldato
Joined
8 Nov 2006
Posts
9,237
In VB script, is it possible to pass a function name as an argument which I can then call?

Example:

Function FunctionWhichExecutesFunction (functionToCall)
Execute functionToCall in this case HelloFunction passed form Function1
End Function

Function Function1
FunctionWhichExecutesFunction (HelloFunction)
End Function

Function HelloFunction
'Do something here
End Function

Hope that's clear...
 
No. Why would you need to do so anyway? Just pass an argument and call whichever function you need based on that argument.
 
Well unless Function has suddenly become an argument type then it can't be done and most definitely shouldn't be.

Pass the function name as a string and Select Case if you're intent on going that route.
 
Why shouldn't it be done!? :confused: It is widely supported across many languages including C#, Java, PHP, and more.

Just because VBScript doesn't support it, does not mean it shouldn't be done.
 
In VBS it definitely shouldn't be. It would be a major change to the language. If you want to use an approach that allows it then you should a suitable language such as Java. If you want access to proper Objects based types then Vbs Is not the route you should be taking.

I'm guessing in the OP's case the task could be accomplish using functionality present and re-considering the flow of the script.
 
Thanks for the input.

Was hoping it was possible, but am going to have to find some other means of doing what I want.

And can't use another language due to working on a site (a large one) that is all classic ASP.
 
This is entirely possible in classic ASP/VBScript. You need to use the Eval() function.

I just tested with this code:

Code:
<%
	function runFunc(funcName)
		Eval(funcName)
	end function
	
	function writeDate() 
		response.Write("<p>The date is " & Now() & ".</p>")
	end function
	
	function writeHelloWorld()
		response.Write("<p>Hello world!</p>")
	end function
	
	function writeLine(output)
		response.Write("<p>" & output & "</p>")
	end function
	
	runFunc(writeDate)
	runFunc(writeHelloWorld)
	runFunc(writeLine("This totally works."))
%>

And it output:
Code:
The date is 18/02/2010 21:15:43.

Hello world!

This totally works.

It's not fantastic practice to use this, as it's slow and usually means that the system has not been thought out correctly - use it only as a last resort.
 
Last edited:
Back
Top Bottom