(SOLVED) Dynamically calling class method from a label

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dens20
Posts: 4
Joined: 26 Jul 2014, 17:15

(SOLVED) Dynamically calling class method from a label

26 Jul 2014, 17:28

Hi,

I run into some difficulty. Please if someone can shed some light why can't i run class method inside a label. I have marked the code line in question. I think I can solve this prob with RegisterCallback and DllCall, but I'am trying to understand OOP and wanna do the oop way.

Code: Select all

#Persistent

a := new classA()
a.timerSet()

class classA{
	test(){
		msgbox % "Hello ! classA.test() here"
	}
	timerSet(){
		set(this.test)
	}
}

set(call){
	static method
	method := call
	SetTimer, label1, 500
	Return

	label1:
		msgbox % "Running timer..."
		msgbox % "Calling " method.name " by reference"
		method.()  ; <---------------------------------------------------- why this line dont work ?
		msgbox % "...nothin..."
		msgbox % "Trying directly..."
		classA.test()
		msgbox % "It works..."
		SetTimer, label1, off
	return
}
Last edited by dens20 on 27 Jul 2014, 03:21, edited 1 time in total.
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Dynamically calling class method from a label

26 Jul 2014, 21:23

You didn't pass enough parameters, so it silently fails. When dealing with class methods, there is an implicit this parameter, which refers to the object whose method is being called. Calling it directly with a function reference doesn't pass this for you. If you change the line to method.(a), it works fine. One possible workaround is to store a reference to the object in addition to the method.

Code: Select all

#SingleInstance Force
	
#Persistent

a := new classA()
a.timerSet()

class classA{
    test(){
        msgbox % "Hello ! classA.test() here"
    }
    timerSet(){
        set(this.test)
    }
}

set(call){
    static method
    method := call
    SetTimer, label1, 500
    Return

    label1:
        msgbox % "Running timer..."
        msgbox % "Calling " method.name " by reference"
        method.(a)  ; <---------------------------------------------------- modified
        msgbox % "...works..."
        msgbox % "Trying directly..."
        classA.test()
        msgbox % "It works..."
        SetTimer, label1, off
    return
}
dens20
Posts: 4
Joined: 26 Jul 2014, 17:15

Re: Dynamically calling class method from a label

27 Jul 2014, 03:20

Ty for reply, and solution. I get it now. Did your suggestion on storing object ref. Here's example for anyone to stumble across it

Code: Select all

#SingleInstance Force
#Persistent

a := new someClass()
a.setTimer()

class someClass{
    setTimer(){
    	setTimerForMethod(this,this.test,3000)
    }
    test(){
        msgbox % "Hello ! someClass.test() here`nRunning thru timed label every 3000ms..."
    }
}

setTimerForMethod(classCall,methodCall,period){
    static class,method
    class := classCall
    method := methodCall
    SetTimer, label1, %period%
    Return

    label1:
        method.(class)
    return
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 352 guests