The range of input values possible is quite huge in my case. So a select-case methodology will not work.
So based on my input say flowers.. doffodil,lily,rose,etc, my function name to call would be flowerdoffodil()
, flowerlily()
, flowerrose()
, etc. Ech of these functions are already defined.
Only which one to call will need to be determined at runtime based on my input.
Is there a way to do this in vb script?
Note: I am a rookie programmer and am using QTP for automation.
Asked
Active
Viewed 233 times
0
1 Answers
5
Use GetRef() as in:
Option Explicit
Function flowerdoffodil()
flowerdoffodil = "my name is daffodil!"
End Function
Function flowerlily()
flowerlily = "my name is lily!"
End Function
Function flowerrose()
flowerrose = "my name is rose!"
End Function
Dim aInp : aInp = Split("doffodil lily rose")
Dim sFnc
For Each sFnc In aInp
Dim fncX : Set fncX = GetRef("flower" & sFnc)
Dim sRes : sRes = fncX()
WScript.Echo sFnc, TypeName(fncX), sRes
Next
output:
cscript 30161364.vbs
doffodil Object my name is daffodil!
lily Object my name is lily!
rose Object my name is rose!
Further food for thought: You can use a Dictionary:
Dim dicFncs : Set dicFncs = CreateObject("Scripting.Dictionary")
Set dicFncs("doffodil") = GetRef("flowerdoffodil")
Set dicFncs("lily") = GetRef("flowerlily")
Set dicFncs("rose") = GetRef("flowerrose")
and call the function(s) like:
Dim sRes : sRes = dicFncs(sFnc)()

Community
- 1
- 1

Ekkehard.Horner
- 38,498
- 2
- 45
- 96
-
1Alternative would be using Eval & Co., which would avoid using GetRef (not sure if this is better, but it might be easier to understand the concept of "evaluate this string" than the concept of function pointers/delegates). – TheBlastOne May 12 '15 at 06:48