Dynamically Using a Method/Field in a Class Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Dynamically Using a Method/Field in a Class

25 Oct 2018, 12:57

Not sure if this is intentional, but you cannot do this, or at least not the way I would expect. Example below:

Code: Select all

class C
{
	someField := "hello"
	
	test()
	{
		f := "field"
		m := "method"
		var := this.some%m%("hi")		;Error: 'Ambiguous or invalid use of "."'
		this.some%f% := var			;Error: 'This line does not contain a recognized action.'
	}
	
	someMethod(str)
	{
		return str
	}
}
I have no idea what I'm doing.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Dynamically Using a Method/Field in a Class

25 Oct 2018, 13:04

Topic moved:
You cannot use %% when accessing objects at all.
Always use [] instead.
Recommends AHK Studio
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: Dynamically Using a Method/Field in a Class

25 Oct 2018, 13:41

I'm confused. Are you saying to do something like this?

Code: Select all

var := this["some" . m]("hi")
this["some" . f] := var
It runs now, but it doesn't do anything. It doesn't call someMethod, and doesn't change someField.
I have no idea what I'm doing.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Dynamically Using a Method/Field in a Class  Topic is solved

25 Oct 2018, 13:51

yes and it does:

Code: Select all

class C
{
	someField := "hello"
	
	test()
	{
		f := "Field"
		m := "method"
		MsgBox % this.someField
		var := this["some" m]("hi")		;Error: 'Ambiguous or invalid use of "."'
		MsgBox % var
		this["some" f] := var			;Error: 'This line does not contain a recognized action.'
		MsgBox % this.someField
	}
	
	someMethod(str)
	{
		return str
	}
}

Obj := new C()
Obj.test()
MsgBox % Obj.someField
MsgBox exit
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: Dynamically Using a Method/Field in a Class

25 Oct 2018, 13:59

Oh derp, my bad I was just doing obj = new C(), not :=

Thanks to both of you for helping.
I have no idea what I'm doing.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot] and 290 guests