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

3

3

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

assume functionname is going to have a value say "sayhi". Can i use that value to call the function ? like i did in the code "call functionname".

I know it is not working but how to do such call ?

Glendaglenden answered 4/10, 2011 at 7:57 Comment(0)
P
7
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 the datatable.

Parsley answered 4/10, 2011 at 8:6 Comment(3)
just build the vs command line you want in a string variable.Parsley
great answer . .what if these methods are in a vbs file which is outisde the same file. it is not picking up the methods in that file . .Glendaglenden
There is no include in vbs. If you want to run a different file you have also to load the file into the string called by executeParsley
S
12

Use GetRef() to get a 'pointer'/reference to a 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
Stacee answered 4/10, 2011 at 8:58 Comment(1)
With GetRef, you can pass a pointer to a function/sub which is outside of the scope of the code that is calling it via the pointer. With execute, it must be within scope. That´s a decent difference, +1 for the GetRef idea which is far less popular in conjunction with QTP imho.Flagwaving
P
7
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 the datatable.

Parsley answered 4/10, 2011 at 8:6 Comment(3)
just build the vs command line you want in a string variable.Parsley
great answer . .what if these methods are in a vbs file which is outisde the same file. it is not picking up the methods in that file . .Glendaglenden
There is no include in vbs. If you want to run a different file you have also to load the file into the string called by executeParsley
V
0

Someone mentioned there was no Include in VBS but you can code one with...

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

Vulpecula answered 23/9, 2019 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.