Page 1 of 1

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

Posted: 10 Apr 2022, 12:46
by logan9

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

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

Posted: 11 Apr 2022, 07:31
by Cuadrix

Code: Select all

This.Timer := Func("CCC.onHover").Bind(0, 0, 0, hWnd)            
SetTimer, % This.Timer, 150

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

Posted: 11 Apr 2022, 09:09
by Helgef
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.

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

Posted: 11 Apr 2022, 10:47
by Rohwedder
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
}

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

Posted: 11 Apr 2022, 11:45
by Helgef
How would you apply this recommendation here?
Hallo, the recommendation doesn't apply here, it applies to methods.

Cheers.

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

Posted: 11 Apr 2022, 19:38
by logan9
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

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

Posted: 12 Apr 2022, 03:55
by Helgef
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.

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

Posted: 12 Apr 2022, 09:03
by doubledave22
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
    }
}