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

I am trying to use getref to call a function from a function library related to a test. My code is

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 get an error -

"Invalid call or procedure argument."

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

+6
source share
2 answers

Minimalist 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 call or procedure argument:" GetRef "indicates that the library of functions (import) is to blame.

Update:

I think it can be assumed that the VBScript mechanism supports matching names under / functions / methods with the called code to make alphabetic / direct calls:

 n = Twice(11) 

and that GetRef ("Double") accesses this table. Therefore, I never expected the call to the indirect / "function pointer" or GetRef () to fail when the literal call was successful.

But according to this and which exists , there are at least four ways to β€œimport” libraries / modules into QTP, and since I do not use QTP, I cannot exclude that some (or even all) of these methods do something stupid to cause the behavior you described is incorrect.

+5
source

I ran into the same problem and found out that linking or loading functional libraries does not work for GetRef . To work around this problem, all you have to do is not bind or load the FL, execute it using the ExecuteFile function.

In action1

 ExecuteFile "/path/functionallibrary.vbs" str = "sample" msg = "hi" x = GetRef("Function_" & str)(msg) msgbox x 
+2
source

Source: https://habr.com/ru/post/945577/


All Articles