Getting a function reference to a method in a class you are writing Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
JoeSchmoe
Posts: 52
Joined: 08 Dec 2014, 08:58

Getting a function reference to a method in a class you are writing

25 Sep 2020, 19:32

How do I get a function reference to a method in a class I am writing?

For example, I need a reference to "mycallback" to send to "OnEvent" in "__New," below. How do I get it?

Code: Select all

global MyGuiObj := MyGUIClass.new()

class MyGUIClass
{
   __new() {
		this.MyGui := Gui.New() 
		this.MyEdit := this.MyGui.Add("Edit")
		this.MyEdit.OnEvent("Change", this.mycallback)
	}
	mycallback(GuiCtrlObj, Info){
	}
}
Thanks for any help in advance.
JoeSchmoe
Posts: 52
Joined: 08 Dec 2014, 08:58

Re: Getting a function reference to a method in a class you are writing

25 Sep 2020, 20:20

Hey everyone, Cloakersmoker helped me with this. Here is the solution:

Code: Select all

global MyGuiObj := MyGUIClass.new()

class MyGUIClass
{
   __new() {
        this.MyGui := Gui.New() 
        this.MyEdit := this.MyGui.Add("Edit")
        this.MyEdit.OnEvent("Change", this.GetMethod('mycallback').Bind(this))
    }
    mycallback(GuiCtrlObj, Info){
    }
}
swagfag
Posts: 4092
Joined: 11 Jan 2017, 17:59

Re: Getting a function reference to a method in a class you are writing

26 Sep 2020, 01:50

Code: Select all

global MyGuiObj := MyGUIClass.New()

class MyGUIClass extends Gui
{
	__New() {
		super.__New(, , this)
		this.Add('Edit').OnEvent('Change', 'mycallback')
		this.Show()
	}

	mycallback(GuiCtrlObj, Info){
	}
}
JoeSchmoe
Posts: 52
Joined: 08 Dec 2014, 08:58

Re: Getting a function reference to a method in a class you are writing

26 Sep 2020, 21:59

I'm curious, why does your code work when the following code doesn't?

Code: Select all

class MyGUIClass
{
   __new() {
		this.MyGui := Gui.New() 
		this.MyEdit := this.MyGui.Add("Edit")
		this.MyEdit.OnEvent('Change', 'mycallback')
	}
	mycallback(GuiCtrlObj, Info){
	}

}
It was misleading to call the class MyGUIClass. It's more of a complex framework involving several GUIs that I interact with in a variety of different ways and at different times. At least one of the GUIs will need to be a property of the class framework.

Is there something I should be reading that would help me understand this? I'm not able to keep up with all of the development discussions. I used to program C in the 90s, but I'm pretty bad with OOP, frankly.
Last edited by JoeSchmoe on 31 Oct 2020, 22:32, edited 1 time in total.
User avatar
FredOoo
Posts: 118
Joined: 07 May 2019, 21:58
Location: Paris

Re: Getting a function reference to a method in a class you are writing  Topic is solved

27 Sep 2020, 23:48

Code: Select all

MyGuiObj := MyGUIClass.New()

class MyGUIClass
{
   __new() {
		this.MyGui := Gui.New()
		this.MyEdit := this.MyGui.Add("Edit")
		this.MyEdit.OnEvent('Change', 'mycallback')
		this.MyGui.Show()
	}
}
mycallback(GuiCtrlObj, Info){
	SoundBeep
}

ObjBindMethod builds a reference to mycallback in this.

Code: Select all

MyGuiObj := MyGUIClass.New()

class MyGUIClass
{
   __new() {
		this.MyGui := Gui.New()
		this.MyEdit := this.MyGui.Add("Edit")
		this.MyEdit.OnEvent('Change', ObjBindMethod(this,'mycallback'))
		this.MyGui.Show()
	}
	mycallback(GuiCtrlObj, Info){
		SoundBeep
	}
}

You should notice that MyGuiObj is not a Gui. It's just a class contening a Gui in this.MyGui.

If you use class MyGUIClass extends Gui, then MyGUIClass is a Gui.

Code: Select all

MyGuiObj := MyGUIClass.New()

class MyGUIClass extends Gui
{
	__New() {
		super.__New(, , this)
		this.Add('Edit').OnEvent('Change', 'mycallback')
		this.Show()
	}
	mycallback(GuiCtrlObj, Info){
		SoundBeep
	}
}

When you call this.Show(), witch doesn't exists in MyGUIClass, it calls Gui.Show()
But:
You can't call this.__New(… from inside __New( itself because it will loop ! So you call super.__New(… to specificaly call Gui.__New(.

When you give the argument this in super.__New(, , this) you say:
Events named in OnEvent(… are all in this. So you don't need to use ObjBindMethod(this,'mycallback').

mycallback should better be named Edit_Change.

If you come back here, you should declare this topic solved by swagfag.
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
JoeSchmoe
Posts: 52
Joined: 08 Dec 2014, 08:58

Re: Getting a function reference to a method in a class you are writing

31 Oct 2020, 22:40

Thanks, Fred, that's perfect. I actually like your solution more, so have marked it as the solution.

I'm currently using your version rather than Cloakersmokers. Both actually work. Are they equivalent?

Code: Select all

this.MyEdit.OnEvent("Change", this.GetMethod('mycallback').Bind(this))   ; Cloakersmoker
this.MyEdit.OnEvent('Change', ObjBindMethod(this,'mycallback'))   ; FredOoo
swagfag
Posts: 4092
Joined: 11 Jan 2017, 17:59

Re: Getting a function reference to a method in a class you are writing

01 Nov 2020, 06:58

no. one requires that a method by the specified name exists in one of the object's bases(or their respective prototypes) at the time of calling. the other one does not.
both suck if u care about objects remaining destructible without having to manually clear out circular references

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 2 guests