function%myvar%() Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Guhl
Posts: 25
Joined: 29 May 2018, 01:12

function%myvar%()

30 May 2018, 15:10

Hi,

myvar := "abc" ; assign abc to myvar
myfunct_%myvar%() ;call the function with name myfunct_abc - working fine
myfunct_%myvar%()
{
Msgbox "hi"
}

Error at line 3.
The following function name contains an illegal character: "myfunct_%myvar%"
The program will exit.

This one works fine:
myfunct_abc()
{
Msgbox "hi"
}

Is there a way to make the function itself have a var in the name or a workaround? I need to have the same function over and over again but it needs to have unique names when calling
swagfag
Posts: 4036
Joined: 11 Jan 2017, 17:59

Re: function%myvar%()  Topic is solved

30 May 2018, 15:46

Foot, meet shotgun. Shotgun, foot.

Code: Select all

greeter(origin) {
	MsgBox % "Hello from: " origin
}

var1 := "abc"
myFunc_%var1% := Func("greeter").Bind(var1)

var2 := "def"
myFunc_%var2% := Func("greeter").Bind(var2)

myFunc_%var1%.Call()
myFunc_%var2%.Call()
User avatar
Guhl
Posts: 25
Joined: 29 May 2018, 01:12

Re: function%myvar%()

30 May 2018, 16:48

thank you :) works flawlessly :) but also found that

Code: Select all

myfunct_"%myvar%"() ; just " before and after the % sign
{
Msgbox "hi"
}
does the trick :)
I found already a good use for your code :)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: function%myvar%()

30 May 2018, 16:51

Guhl wrote:I need to have the same function over and over again but it needs to have unique names when calling
My imagination cannot come up with any handy use case of such functions...

Here's another way to "make the function itself have a var in the name". However, I suggest creating Func Objects, has demonstrated by swagfag. Moreover, unlike swagfag's one, the following version does not support ByRef parameters:

Code: Select all

var1 := "abc"
MsgBox, 64,, % myFunc_%var1%("a", "b", "c")
var1 := "def"
MsgBox, 64,, % myFunc_%var1%("a", "b")
var1 := "ghi"
MsgBox, 64,, % myFunc_%var1%()


f(_funcName, _o, _params*) {
	static _ := ("".base.__Call:=Func("f"))
return Func("greeter").call(_funcName, _params*)
}
greeter(_origin, _params*) {
MsgBox % "Hello from: " . _origin
for _each, _param in _params
	MsgBox % _each "," _param
return SubStr(_origin, -2)
}
my scripts
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: function%myvar%()

30 May 2018, 16:59

Guhl wrote:thank you :) works flawlessly :) but also found that

Code: Select all

myfunct_"%myvar%"() ; just " before and after the % sign
{
Msgbox "hi"
}
does the trick :)
Your code does not call any function...

Code: Select all

myvar := "abc" ; assign abc to myvar
myfunct_"%myvar%"() ; just " before and after the % sign
{
Msgbox "hi"
}
myfunct_abc() {
MsgBox % A_ThisFunc
}
ListLines
Pause
my scripts

Return to “Ask For Help”

Who is online

Users browsing this forum: blured, hasantr, mikeyww, Smile_, sobuj53, SOTE, Xsawers and 55 guests