Problem with Class

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
stretch65
Posts: 21
Joined: 16 Jun 2015, 23:49

Problem with Class

Post by stretch65 » 08 Nov 2019, 00:19

Hi,

I can't figure out how to get this code working. In the example below,
I'm trying to add an item to a menu, and that item should call the static
method 'doStuff'. But no matter what I try it's not working. Can someone
help?

Code: Select all

MyClass.buildMenu()

msgbox "Script Loaded"

; --------------------------------------------------    

class MyClass {
  static myMenu := menuCreate()
  
  static buildMenu() {
    MyClass.myMenu.add("Menu Text", MyClass.doStuff)
  }

  static doStuff() {
    msgbox "Inside 'doStuff()'"
  }
}

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Problem with Class

Post by Helgef » 08 Nov 2019, 02:09

MyClass.doStuff, this is attempting to retrieve a non existent property, you need to use getmethod(), and then you bind the class to the first parameter, that is, to this.

Cheers.

stretch65
Posts: 21
Joined: 16 Jun 2015, 23:49

Re: Problem with Class

Post by stretch65 » 08 Nov 2019, 03:30

OK! Thank you so much. I've changed the code to the following (with no error messages):

Code: Select all

MyClass.buildMenu()

msgbox "Script Loaded"

; --------------------------------------------------    

class MyClass {
  static myMenu := menuCreate()
  
  static buildMenu() {
    MyClass.myMenu.add("Menu Text", MyClass.getMethod("doStuff"))
  }

  static doStuff() {
    msgbox "Inside 'doStuff()'"
  }
}
Sorry if I'm bit dim but what do you mean by "...bind the class to the first parameter, that is, to this"?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Problem with Class

Post by swagfag » 08 Nov 2019, 04:49

Code: Select all

static buildMenu() {
  MyClass.myMenu.add("Menu Text", MyClass.getMethod("doStuff").Bind(MyClass))
}

static doStuff(ItemName, ItemPos, Menu) {
  msgbox "Inside 'doStuff()'"
}
or

Code: Select all

static buildMenu() {
  MyClass.myMenu.add("Menu Text", (ItemName, ItemPos, Menu) => MyClass.doStuff())
}

static doStuff() {
  msgbox "Inside 'doStuff()'"
}

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Problem with Class

Post by Helgef » 09 Nov 2019, 02:51

stretch65 wrote:I've changed the code to the following (with no error messages):
You will get an error message when you attempt to invoke the menu item's callback, because you have not defined the required parameters. If you do, as shown above,

Code: Select all

static doStuff(ItemName, ItemPos, Menu) {
  msgbox "Inside 'doStuff()'"
}
You'll still get an error message because ahk passes three parameters, but the method has a fourth, hidden parameter, that is this. You'll have to fill it in yourself as it is only implicitly passed when using the method call syntax, i.e., o.m(). So use bind() as shown by swagfag. Some examples to show,

Code: Select all

class c {
	static g(){
		msgbox this == c 
			? 'this == c is true' 
			: 'this is the string:`t"' . this . '"'
	}
}

c.g()			; "normal call"

g := c.getmethod('g')
%g%('abc')		; call func reference, when you pass a reference to any built in function such as settimer or onmessage,
				; this is how it will be called and behave.

g := g.bind(c)
%g%()			; call a boundfunc, behaves as normal call if bound "correctly".
				; Pass boundfuncs to built in functions to make the callback behave like normal calls.
Also see :arrow: Function objects.

Cheers.

Post Reply

Return to “Ask for Help (v2)”