Call a function whose name is stored in an associative array
Call a function whose name is stored in an associative array
I know I can call a function whose name is stored in a variable, using the syntax %Var%(), but I can't find the right syntax to use when the name is stored in an associative array. Unless the error is somewhere else it's not % Array[Key](). Can anyone help me out?
Re: Call a function whose name is stored in an associative array
You wrap the expression in %, just like with the simple variable, so it would be %Array[Key]%().
Demonstration:
Demonstration:
Code: Select all
#Requires AutoHotkey v2.0
FuncNames := Map('MyFunc', 'Func1', 'YourFunc', 'Func2')
%FuncNames['MyFunc']%()
%FuncNames['YourFunc']%()
Func1() {
MsgBox 'This is function 1'
}
Func2() {
MsgBox 'This is function 2'
}
Re: Call a function whose name is stored in an associative array
Hallo.
Or, as this is the v1.1 and older forum:
Or, as this is the v1.1 and older forum:
Code: Select all
#Requires AutoHotkey v1.1.33
FuncNames := {MyFunc:"Func1", YourFunc:"Func2"}
FuncNames.MyFunc()
FuncNames.YourFunc()
; or
; FuncNames["MyFunc"]()
; FuncNames["YourFunc"]()
Func1() {
MsgBox This is function 1
}
Func2() {
MsgBox This is function 2
}
Re: Call a function whose name is stored in an associative array
autohotkey has these things called function objects, you can also put those in an array and call them like so:
You may find them documented here: https://www.autohotkey.com/docs/v1/lib/Func.htm
Code: Select all
funcObj := func("myCoolFunc")
myArray := {"func1": funcObj, "func2": funcObj, "func3": funcObj}
myArray["func2"].call(10)
; => 20
myCoolFunc(parameter) {
msgbox, % parameter * 2
}
Who is online
Users browsing this forum: Spawnova and 278 guests