Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

qtp - 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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...