Jump to content
Sign in to follow this  
nf67

Calling functions using a variable as func name

Recommended Posts

nf67

Hi there,

I was just wondering if this is possible, calling a func using its name which is stored in a variable?

Here's a quick example of what I mean:

InformAboutFunc(LaunchNotePad)

Func InformAboutFunc($Function)
    MsgBox(0, "MSG", "Going to use function " & $Function & ".")
    $Function ; perhaps add & () ?      
EndFunc

Func LaunchNotePad()
    Run("notepad.exe")
EndFunc

It works if I replace $Function with LaunchNotePad() (also have to use quotation marks around LaunchNotePad in the first line).

Since $Function equals LaunchNotePad in this script, why wouldn't I be able to call the func like that?

Thanks :D

/curious

Share this post


Link to post
Share on other sites
dantay9

This seems to work. Is this what you wanted?

InformAboutFunc("LaunchNotePad")

Func InformAboutFunc($Function)
    MsgBox(0, "MSG", "Going to use function " & $Function & ".")
    Call($Function)
EndFunc  ;==>InformAboutFunc

Func LaunchNotePad()
    Run("notepad.exe")
EndFunc  ;==>LaunchNotePad

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
nf67

Yes exactly, thanks :D .

Share this post


Link to post
Share on other sites
ResNullius

I'm trying to do something similar with this code, but it isn't working. I want to call a function using a variable name based on the combined text of the radio and button controls. Is this possible? If so, then why isn't it working? I've tried moving the Call() statement to various other lines, but it never works.

Opt("MustDeclareVars", 1)
Global $aRadio[11] = [10], $X

GUICreate("Title", 200, 200)
Local $button = GUICtrlCreateButton("Go", 0, 150, 200, 50, 0x0F00)

GUICtrlCreateTabItem("One")
$aRadio[01] = GUICtrlCreateRadio("Fish", 9, 33, 180, 20)

GUISetState(@SW_SHOW)

While 1
    Local $msg = GUIGetMsg()
    Select
        Case $msg = -3
            Exit
        Case $msg = $button
            Go()
    EndSelect
WEnd

Func Go()
    If GUICtrlRead($aRadio[01]) = 1 Then
        $X = '"' & ControlGetText("Title", "", $button) & "_" & ControlGetText("Title", "", $aRadio[01]) & '"'
        MsgBox(0,"",$X)
    EndIf
    Call($X)
EndFunc

Func Go_Fish()
    MsgBox(0,"Test","Test")
EndFunc
You don't need to put quotes around the string you're building by reading the controls.

$X = ControlGetText("Title", "", $button) & "_" & ControlGetText("Title", "", $aRadio[01])

So your "Go" Func should end up like this:

Func Go()
    If GUICtrlRead($aRadio[01]) = 1 Then
        $X = ControlGetText("Title", "", $button) & "_" & ControlGetText("Title", "", $aRadio[01])
        MsgBox(0,"",$X)
    Call($X)
    EndIf
EndFunc

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
Sign in to follow this  

×