Page 1 of 1

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

Posted: 25 Sep 2020, 19:32
by JoeSchmoe
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.

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

Posted: 25 Sep 2020, 20:20
by JoeSchmoe
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){
    }
}

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

Posted: 26 Sep 2020, 01:50
by swagfag

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){
	}
}

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

Posted: 26 Sep 2020, 21:59
by JoeSchmoe
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.

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

Posted: 27 Sep 2020, 23:48
by FredOoo

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.

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

Posted: 31 Oct 2020, 22:40
by JoeSchmoe
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

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

Posted: 01 Nov 2020, 06:58
by swagfag
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