[SOLVED] How to call a method using gui with or without creating an instance from it's class

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

[SOLVED] How to call a method using gui with or without creating an instance from it's class

09 Feb 2016, 15:55

Hello,

Can anyone please explain in details how to run MyMethod() from the gui in the code bellow, and retrieve the value of MyAttr possibly without creating a new object from MyClass()? The method is being triggered by clicking on the checkbox in the first tab of the gui.

Code: Select all

Class MyClass {
	static MyAttr := "sometext"
	MyMethod() {
		MsgBox, % this.MyAttr
	}
}

Gui, Add, Tab2,, First Tab|Second Tab|Third Tab  ; Tab2 vs. Tab requires v1.0.47.05.
Gui, Add, Checkbox, vMyCheckbox gMyClass.MyMethod, Sample checkbox
Gui, Tab, 2
Gui, Add, Radio, vMyRadio, Sample radio1
Gui, Add, Radio,, Sample radio2
Gui, Tab, 3
Gui, Add, Edit, vMyEdit r5
Gui, Tab
Gui, Add, Button, default xm, OK
Gui, Show
return

ButtonOK:
GuiClose:
GuiEscape:
ExitApp
Last edited by phaleth on 14 Feb 2016, 04:09, edited 1 time in total.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: How to call a method using gui with or without creating an instance from it's class

09 Feb 2016, 19:55

You need to use GuiControl to associate a function object, see: GuiControl(Functor)
Example:

Code: Select all

Gui, Add, Checkbox, vMyCheckbox, Sample checkbox
	method := ObjBindMethod(MyClass, "MyMethod")
	; or method := MyClass.MyMethod.Bind(MyClass)
	GuiControl +g, MyCheckbox, %method%
wdmodlin
Posts: 150
Joined: 16 Dec 2015, 02:42

Re: How to call a method using gui with or without creating an instance from it's class

09 Feb 2016, 21:43

I am just now struggling to learn this sort of thing myself.

But I am pretty sure that you can access things in a class, without creating instances, just by using the class name.
Class names are Global.

So you can say

Code: Select all

   x :=   MyClass.MyAttr
to read the value
and

Code: Select all

 MyClass.MyMethod()
to execute a method.

For clarity you might also use your class name instead of "this" inside the methods when you are referring to values defined as static in the base class itself. So I would code it as:

Code: Select all

MyMethod() {
		MsgBox, % MyClass.MyAttr
	}
reserving "this" for things that are actually specific to a particular instance.

Lexikos will correct me if I am wrong. :)
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

Re: How to call a method using gui with or without creating an instance from it's class

10 Feb 2016, 11:30

Coco wrote:You need to use GuiControl to associate a function object, see: GuiControl(Functor)
Example:

Code: Select all

Gui, Add, Checkbox, vMyCheckbox, Sample checkbox
	method := ObjBindMethod(MyClass, "MyMethod")
	; or method := MyClass.MyMethod.Bind(MyClass)
	GuiControl +g, MyCheckbox, %method%
Thanks for the working code, Coco. To be honest i don't understand a single word that the docs say on this matter. Is the method some kind of reference to the MyMethod() of the MyClass()? Isn't that the same thing as creating an instance as the this keyword works properly?

Here is the full code:

Code: Select all

Class MyClass {
	static MyAttr := "sometext"
	MyMethod() {
		MsgBox, % this.MyAttr
	}
}

Gui, Add, Tab2,, First Tab|Second Tab|Third Tab  ; Tab2 vs. Tab requires v1.0.47.05.
Gui, Add, Checkbox, vMyCheckbox, Sample checkbox
method := ObjBindMethod(MyClass, "MyMethod")
; or method := MyClass.MyMethod.Bind(MyClass)
GuiControl +g, MyCheckbox, %method%
Gui, Tab, 2
Gui, Add, Radio, vMyRadio, Sample radio1
Gui, Add, Radio,, Sample radio2
Gui, Tab, 3
Gui, Add, Edit, vMyEdit r5
Gui, Tab
Gui, Add, Button, default xm, OK
Gui, Show
return

ButtonOK:
GuiClose:
GuiEscape:
ExitApp
wdmodlin wrote:For clarity you might also use your class name instead of "this" inside the methods when you are referring to values defined as static in the base class itself. So I would code it as:

Code: Select all

MyMethod() {
		MsgBox, % MyClass.MyAttr
	}
reserving "this" for things that are actually specific to a particular instance.

Lexikos will correct me if I am wrong. :)
Yeah, you are right, wdmodlin, this keyword should only work for instances, never for the class itself. It was just my silly attempt to hammer the code.

Thank you.

Here is the working code:

Code: Select all

Class MyClass {
	static MyAttr := "sometext"
	MyMethod() {
		MsgBox, % MyClass.MyAttr
	}
}

Gui, Add, Tab2,, First Tab|Second Tab|Third Tab  ; Tab2 vs. Tab requires v1.0.47.05.
Gui, Add, Checkbox, vMyCheckbox gMyClass.MyMethod, Sample checkbox
Gui, Tab, 2
Gui, Add, Radio, vMyRadio, Sample radio1
Gui, Add, Radio,, Sample radio2
Gui, Tab, 3
Gui, Add, Edit, vMyEdit r5
Gui, Tab
Gui, Add, Button, default xm, OK
Gui, Show
return

ButtonOK:
GuiClose:
GuiEscape:
ExitApp

Is there any way to avoid the need to use class name itself? Something like the following?

Code: Select all

	MyMethod() {
		MsgBox, % self.MyAttr
	}
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: [SOLVED] How to call a method using gui with or without creating an instance from it's class

19 Feb 2016, 13:27

phaleth wrote:Here is the working code:

Code: Select all

Class MyClass {
	static MyAttr := "sometext"
	MyMethod() {
		MsgBox, % MyClass.MyAttr
	}
}

Gui, Add, Tab2,, First Tab|Second Tab|Third Tab  ; Tab2 vs. Tab requires v1.0.47.05.
Gui, Add, Checkbox, vMyCheckbox gMyClass.MyMethod, Sample checkbox
Gui, Tab, 2
Gui, Add, Radio, vMyRadio, Sample radio1
Gui, Add, Radio,, Sample radio2
Gui, Tab, 3
Gui, Add, Edit, vMyEdit r5
Gui, Tab
Gui, Add, Button, default xm, OK
Gui, Show
return

ButtonOK:
GuiClose:
GuiEscape:
ExitApp

Is there any way to avoid the need to use class name itself? Something like the following?

Code: Select all

	MyMethod() {
		MsgBox, % self.MyAttr
	}
In your working code, gMyClass.MyMethod is associating the method/function by 'name' which should not be done for class methods. The method gets called but the variable this does not contain a reference to an object. It works but might as well use a normal function or label if you're going to do that. To acquire a callable object that calls the method, you can:
  • method := YourClass.Method returns a Func object -> when assigning this using GuiControl +g, the behavior is the same as gYourClass.Method so it's best not to associate the returned object with the control
  • method := YourClass.Method.Bind(YourClass) or method := ObjBindMethod(YourClass, "Method") bind the class object itself as the first parameter. From within the method, the variable this contains a reference to the class object itself
  • method := YourClass.Method.Bind(new YourClass) or method := ObjBindMethod(new YourClass, "Method") same as above but an instance of the class is used
To attach a class method to a GuiControl, use either example 2 or 3. It's best to use a BoundFunc object especially when you need a reference to the object(class or instance) from within the method. Then use GuiControl +g as the docs suggested.

Here is an example that demonstrates the difference between gYourClass.Method and GuiControl +g, ControlID, %callable_object%:

Code: Select all

Gui, Add, Button, w100 r1 gFoo.Method, Button 1
Gui, Add, Button, y+10 wp hp, Button 2
	foo_method := ObjBindMethod(new Foo, "Method")
	GuiControl +g, Button2, %foo_method%
Gui, Show
return
GuiClose:
ExitApp

class Foo
{
	Method()
	{
		MsgBox %A_ThisFunc% ; Fucntion/method "name"
		MsgBox % IsObject(this) ? "'this' is an object" : "'this' is not an object"
		if IsObject(this) ; if we bind an object(class or instance), we can call other methods of the class
			this.AnotherMethod()
	}

	AnotherMethod()
	{
		MsgBox %A_ThisFunc%
	}
}
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: [SOLVED] How to call a method using gui with or without creating an instance from it's class

19 Feb 2016, 17:44

phaleth wrote:Yeah, you are right, wdmodlin, this keyword should only work for instances, never for the class itself.
There's nothing wrong with using this to refer to the class itself when that's what it contains; i.e. for static method calls like MyClass.MyMethod(). wdmodlin just suggested that it might be clearer to use the class name. It's a matter of personal preference.
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

Re: [SOLVED] How to call a method using gui with or without creating an instance from it's class

20 Feb 2016, 02:47

Coco wrote:In your working code, gMyClass.MyMethod is associating the method/function by 'name' which should not be done for class methods. The method gets called but the variable this does not contain a reference to an object. It works but might as well use a normal function or label if you're going to do that. To acquire a callable object that calls the method, you can:
  • method := YourClass.Method returns a Func object -> when assigning this using GuiControl +g, the behavior is the same as gYourClass.Method so it's best not to associate the returned object with the control
  • method := YourClass.Method.Bind(YourClass) or method := ObjBindMethod(YourClass, "Method") bind the class object itself as the first parameter. From within the method, the variable this contains a reference to the class object itself
  • method := YourClass.Method.Bind(new YourClass) or method := ObjBindMethod(new YourClass, "Method") same as above but an instance of the class is used
To attach a class method to a GuiControl, use either example 2 or 3. It's best to use a BoundFunc object especially when you need a reference to the object(class or instance) from within the method. Then use GuiControl +g as the docs suggested.

Here is an example that demonstrates the difference between gYourClass.Method and GuiControl +g, ControlID, %callable_object%:

Code: Select all

Gui, Add, Button, w100 r1 gFoo.Method, Button 1
Gui, Add, Button, y+10 wp hp, Button 2
	foo_method := ObjBindMethod(new Foo, "Method")
	GuiControl +g, Button2, %foo_method%
Gui, Show
return
GuiClose:
ExitApp

class Foo
{
	Method()
	{
		MsgBox %A_ThisFunc% ; Fucntion/method "name"
		MsgBox % IsObject(this) ? "'this' is an object" : "'this' is not an object"
		if IsObject(this) ; if we bind an object(class or instance), we can call other methods of the class
			this.AnotherMethod()
	}

	AnotherMethod()
	{
		MsgBox %A_ThisFunc%
	}
}
Brilliant explanation! Thanks, Coco.


@Lexikos: Could you possibly add that Coco's last example into docs? Not sure where, It touches on so many things.
murataygun
Posts: 104
Joined: 07 Aug 2015, 15:53

Re: [SOLVED] How to call a method using gui with or without creating an instance from it's class

03 Aug 2021, 06:29

How can i bound function to guiClose ? I just want to destroy the gui and the object when user clics to X button on the top right corner of the window.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 135 guests