Forum Discussion

venkies2's avatar
venkies2
New Contributor
11 years ago

How to call a function in VB script. The function name is in some variable.

I have excel sheet in which contains list of all functions in one of the column (FunctionName) as shown below.





























TestCase

FunctionName

Flag

Parameter1

Parameter2

Parameter3

Scenario

login

Yes

http://xyz.com

 

 

Scenario1

search

Yes

pen

paper

 

 

Script created in such a way that it takes function name and parameters into one variable. After taking into one variable am using to call with call statement. But it not working.

 

While  MainrsObj.EOF<>True

                                               

                        cnt=0

                                    If (MainrsObj.Fields("Flag"))="Yes" Then

                                                 For i=1 to 10

                                                             If (MainrsObj.Fields("Parameter"&i))<>"" Then

                                                                         ReDim preserve Parameters(cnt)

                                                                         Parameters(cnt)=chr(34) &MainrsObj.Fields("Parameter"&i)&chr(34)

                                                                       

                                                                         else

                                                                        Exit For

                                                             End If

                                                             cnt=cnt+1

                                                 Next

                                       totParameters=join(Parameters,",")

                        'msgbox totParameters

                                       FunctionName=cstr(MainrsObj.Fields("FunctionName")) &"  "&totParameters

                       

                                    call  FunctionName

4 Replies

  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor
    Hi Venkateswar



    You could try using the eval statement.  Something like this:





    eval ("Call " & FunctionName)

  • venkies2's avatar
    venkies2
    New Contributor
    Thanks for responding, i tried with this method also but it is not working.....
  • Try Runner.CallMethod



    Here is example of function calling using variable as name






    Function LogMe( SomeText )


    Log.Message( "I'm here!" & " " & SomeText )


    End Function


     


    Function StartMe


    Dim FunctionName


    FunctionName = "Unit1.LogMe" ' full notation of function to be called: unit and name


    Runner.CallMethod FunctionName, "Say This!"


    End Function





    The only drawback of this solution is that you have to specify unit name where your function is stored.

    More detailed description of this method is here.
  • Ravik's avatar
    Ravik
    Super Contributor
    venkateswar bonagiri



    You can also try USEUNIT Method it's may help you



    Syntax -



    'USEUNIT unitName



    like



    'USEUNIT Login



    lets consider your unit name is "login" in that unit u have declare function like - "userLogin"



    Now read parameter from the Excel and Check function name and Flag Status like below -



    'USEUNIT Login



    Function MasterScript



    If FunctionName = userLogin ND FlagStatus = Yes Then

        Call login

    End If



    End Function