How to use a "this.variable" as a function parameter?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
logan9
Posts: 33
Joined: 22 Feb 2022, 12:48

How to use a "this.variable" as a function parameter?

10 Apr 2022, 12:46

Code: Select all

This.Timer := Func("CCC.onHover").Bind(0, 0, 0, hWnd)            
SetTimer, %This.Timer%, 150 ; <---- ??
how to use the This.Timer variable on the SetTimer?

my doubt if is possible to do it other than:

Code: Select all

This.Timer := Func("CCC.onHover").Bind(0, 0, 0, hWnd)
x := this.timer       
SetTimer, %x%, 150
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: How to use a "this.variable" as a function parameter?

11 Apr 2022, 07:31

Code: Select all

This.Timer := Func("CCC.onHover").Bind(0, 0, 0, hWnd)            
SetTimer, % This.Timer, 150
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to use a "this.variable" as a function parameter?

11 Apr 2022, 09:09

If not a valid label name, this parameter can be the name of a function whose parameter list has no mandatory parameters (see the function example), or a single variable reference containing a function object. For example, SetTimer %FuncObj%, 1000 or SetTimer % FuncObj, 1000. Other expressions which return objects are currently unsupported
Additionally, it is recommended to not use Func("CCC.onHover"), use CCC.onHover.bind(...) instead.
methods wrote:Methods are stored by reference in the class object.
Rohwedder
Posts: 7705
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to use a "this.variable" as a function parameter?

11 Apr 2022, 10:47

Hallo,
Additionally, it is recommended to not use Func("CCC.onHover"), use CCC.onHover.bind(...) instead.
How would you apply this recommendation here?:

Code: Select all

#Persistent
Loop, 4
{
	ShortBeep := Func("ShortBeep").Bind(A_Index "000")
	SetTimer,% ShortBeep, 1000
	Sleep, 250
}
ShortBeep(Frequency)
{
	SoundBeep,% Frequency, 20
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to use a "this.variable" as a function parameter?

11 Apr 2022, 11:45

How would you apply this recommendation here?
Hallo, the recommendation doesn't apply here, it applies to methods.

Cheers.
logan9
Posts: 33
Joined: 22 Feb 2022, 12:48

Re: How to use a "this.variable" as a function parameter?

11 Apr 2022, 19:38

Cuadrix wrote:
11 Apr 2022, 07:31

Code: Select all

This.Timer := Func("CCC.onHover").Bind(0, 0, 0, hWnd)            
SetTimer, % This.Timer, 150
I get an error when i try % This.Timer, Error: Parameter #1 invalid.



I also have another doubt, is possible somehow to do the same with an object?

Code: Select all

x := {}
y := 999
x[y] := "ABC"
SetTimer,  % x[y] 150
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to use a "this.variable" as a function parameter?

12 Apr 2022, 03:55

You were correct from the beginning,
my doubt if is possible to do it other than:
No it is not possible. (other than % var vs %var%).

This is what the first quote from the docs in my first post says.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: How to use a "this.variable" as a function parameter?

12 Apr 2022, 09:03

logan9 wrote:
11 Apr 2022, 19:38

Code: Select all

This.Timer := Func("CCC.onHover").Bind(0, 0, 0, hWnd)            
SetTimer, % This.Timer, 150
I get an error when i try % This.Timer, Error: Parameter #1 invalid.
From the docs for settimer "Known limitation: SetTimer requires a plain variable reference."

Code: Select all

counter := new SecondCounter
counter.Start()
Sleep 5000
counter.Stop()
Sleep 2000

; An example class for counting the seconds...
class SecondCounter {
    __New() {
        this.interval := 1000
        this.count := 0
        ; Tick() has an implicit parameter "this" which is a reference to
        ; the object, so we need to create a function which encapsulates
        ; "this" and the method to call:
        this.timer := ObjBindMethod(this, "Tick")
    }
    Start() {
        ; Known limitation: SetTimer requires a plain variable reference.
        timer := this.timer
        SetTimer % timer, % this.interval
        ToolTip % "Counter started"
    }
    Stop() {
        ; To turn off the timer, we must pass the same object as before:
        timer := this.timer
        SetTimer % timer, Off
        ToolTip % "Counter stopped at " this.count
    }
    ; In this example, the timer calls this method:
    Tick() {
        ToolTip % ++this.count
    }
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, EIijah_2cm and 148 guests