Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function (with parameters) which is in function library, taking the function name from a variable?

Tags:

vbscript

qtp

I'm trying to use getref to call a function from the function library associated with the test. My code -

In action1

str = "sample"
msg = "hi"
x = GetRef("Function_"&str)(msg)
msgbox x

In the function Library,

Function Function_sample(strMsg)
    Function_sample = strMsg
End Function

I'm getting the error -

"Invalid procedure call or argument."

But it works fine if the function is placed in the same action. How to call a function (with parameters) which is in function library, taking the function name from a variable?

like image 378
Saranya Avatar asked May 22 '13 12:05

Saranya


People also ask

What do you call a parameters received by a function?

Function Parameters and Arguments Function arguments are the real values passed to (and received by) the function.

What do we call the value provided to a function when the function is called which is assigned to the corresponding parameter in the function )?

argument. A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function. The argument can be the result of an expression which may involve operators, operands and calls to other fruitful functions.

When a function is called by its name during the execution of a program then it is?

When a function is called by its name during the execution of a program, then it is. executed.


2 Answers

Minimalistic working example:

Lib.vbs:

Option Explicit

Function Twice(n)
  Twice = n + n
End Function

Main.vbs:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

ExecuteGlobal goFS.OpenTextFile(".\lib.vbs").ReadAll()

Dim fpTwice : Set fpTwice = GetRef("Twice")

WScript.Echo fpTwice(42)

Output:

cscript main.vbs
84

The error message "... runtime error: Invalid procedure call or argument: 'GetRef'" indicates that the (importing of the) function library is to blame.

Update:

I think that it is plausible to assume that the VBScript engine keeps a table associating sub/function/method names with callable code to be able to do literal/direct calls:

n = Twice(11)

and that GetRef("Twice") accesses this table. So I would never expect an indirect/'function pointer' call or a GetRef() to fail when the literal invocation succeeds.

But according to this and that, there are at least four ways to 'import' libraries/modules into QTP, and as I don't use QTP I can't rule out, that some (or even all) of these methods do something silly to cause the mis-behaviour you describe.

like image 158
Ekkehard.Horner Avatar answered Sep 28 '22 00:09

Ekkehard.Horner


I faced the same issue and found out that associating or loading the functional libraries doesn't work for GetRef. To work around this issue, all you need to do is instead of associating or loading your FL, execute it using ExecuteFile function.

In action1

ExecuteFile "/path/functionallibrary.vbs"
str = "sample"
msg = "hi"
x = GetRef("Function_" & str)(msg)
msgbox x
like image 35
Pankaj Jaju Avatar answered Sep 28 '22 00:09

Pankaj Jaju