StooJ

Call function using a variable

8 posts in this topic

I hope I'm not being too daft and missing something obvious...

I'm trying to create a script that will will be added to in the future. I'm trying to design it so that any additions will be as easy as possible with minimum programming. So, I'm creating functions ( using Func...EndFunc ) and hoping to be able to call them by changing a variable (set from a command line parameter eventually, but declared mid-script for now)

Can I call a function using a variable? Is there a "treat as literal" capability or something similar? Here's what I've got at the moment:

$FuncName = "Test"

$FuncName ()         ; Here's where I'd like to call the function ( in this case Test )

Func Test ()
   Msgbox ( 0 , "Yes" , "It worked!!" )
EndFunc

Func NotThisOne ()
   MsgBox ( 0 , "No" , This function shouldn't be called just now" )
EndFunc

Obviously that won't work, but is there a way for AutoIt to assess the variable and call the correct function? So if $FuncName = "NotThisOne" the second function would be called.

Eventually there will be half a dozen functions at least, and I'm hoping to be able to pick between them by altering the command line parameter.

Many thanks, I hope I've managed to explain what I'm after.

Share this post


Link to post
Share on other sites



you could set all of your functions in a switch statement

Switch $funcname
      case "test"
         Thisfunc ()
       Case "Notthisone"
          Notthisfunc ()
Endswitch

Share this post


Link to post
Share on other sites

I don't think there is a direct function to call Fuctions by name (like there is for variables, Eval). But I do know that some function calls require an input parameter of a second function's name which will get run by the first function, so that sort of functionality IS present (confusing sentence?). Someone else may know how to do that. A work-around:

$FuncName = "Test"

Switch $FuncName
    Case "Test"
        Test()
    Case "NotThisOne"
        NotThisOne()
EndSwitch
    
Func Test ()
   Msgbox ( 0 , "Yes" , "It worked!!" )
EndFunc

Func NotThisOne ()
   MsgBox ( 0 , "No" , "This function shouldn't be called just now" )
EndFunc

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Absolutely perfect, Zedna!

Thank you very much.

Thanks for the case statement as well, folks, but I want to avoid that as it means I need to add more cases as I go.

Edited by StooJ

Share this post


Link to post
Share on other sites

I stand corrected, Call is what I was trying to get across. :)


IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now