Is it possible to call a function whose name is stored in a string in vbscript?

I am trying to write a script in qtp like this

Public Function sayhi msgbox "hi" end Dim level0 dim count1 count1 = DataTable.GetSheet("Action1").GetRowCount msgBox count1 For counterVariable = 1 to count1 functionname = DataTable.value("methodnames","Action1") call functionname DataTable.GetSheet("Action1").SetCurrentRow(counterVariable) Next 

Suppose the function name has the value say sayi. Can I use this value to call a function? as in the code "call functionname".

I know that it does not work, but how to do it?

+3
source share
3 answers
 Option Explicit function abc(a) MsgBox a End function dim run : run = "abc ""Hallo""" execute run 

The execute method can do this.


 Public Function sayhi msgbox "hi" end Dim level0 dim count1 count1 = DataTable.GetSheet("Action1").GetRowCount msgBox count1 For counterVariable = 1 to count1 functionname = "call " & DataTable.value("methodnames","Action1") execute functionname DataTable.GetSheet("Action1").SetCurrentRow(counterVariable) Next 

will call sayhi if its in datatable.

+6
source

Use GetRef () to get a "pointer" / link to Sub or Function:

 Option Explicit Sub S1( s ) WScript.Echo "S1:", GetRef( "F1" )( s ) End Sub Function F1( s ) F1 = UCase( s ) End Function Dim sName : sName = "S1" Dim subS1 : Set subS1 = GetRef( sName ) subS1 "abc" 

output:

 cscript getrefdemo.vbs S1: ABC 
+12
source

Someone mentioned that there is no β€œEnable” in VBS, but you can write code using ...

 Sub Include(yourFile) Dim oFSO, oFileBeingReadIn ' define Objects Dim sFileContents ' define Strings Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFileBeingReadIn = oFSO.OpenTextFile(yourFile & ".vbs", 1) sFileContents = oFileBeingReadIn.ReadAll oFileBeingReadIn.Close ExecuteGlobal sFileContents End Sub 

My source: http://cyreath.blogspot.com/2014/02/vbscript-call-function-in-another-file.html

0
source

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


All Articles