Jump to content

Calling functions using a variable as func name


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...