Page 1 of 1

This value of type "class" has no property named "blah blah"

Posted: 28 Mar 2023, 14:49
by wpb
When I run this code:

Code: Select all

#Requires AutoHotkey v2.0

instance := cInherited("NO OWNER", "My object", "notepad.exe")

class cBase
{
	__New(Owner, objectName)
	{
		this.Owner := Owner
		this.objectName := objectName
	}
}

class cInherited extends cBase
{
	static msDefaultPeriod := 1000

	__New(Owner, objectName, processName)
	{
		super.__New(Owner, objectName)
		; Set up a hover timer, to check for hovering.
		this.msPeriod := this.msDefaultPeriod
	}
}
this.msDefaultPeriod can't be found. AHK reports: This value of type "cInherited" has no property named "msDefaultPeriod".

Why is that?

Re: This value of type "class" has no property named "blah blah"

Posted: 28 Mar 2023, 20:30
by swagfag
because uve declared "msDefaultPeriod" as a static property. the property is an ownproperty of the object "cInherited", so to access it u write "cInherited.msDefaultPeriod"

Re: This value of type "class" has no property named "blah blah"

Posted: 29 Mar 2023, 02:44
by wpb
Thanks - I suspected that might be the case.

But is there no other way? If I were to change the name of the class, I'd have to change all references to its internal static variables.

Re: This value of type "class" has no property named "blah blah"

Posted: 29 Mar 2023, 02:48
by Mono919
wpb wrote:
29 Mar 2023, 02:44
Thanks - I suspected that might be the case.

But is there no other way? If I were to change the name of the class, I'd have to change all references to its internal static variables.
delete "static"

Re: This value of type "class" has no property named "blah blah"

Posted: 29 Mar 2023, 03:27
by wpb
Haha, thanks, I guess that's the obvious solution.

Re: This value of type "class" has no property named "blah blah"

Posted: 29 Mar 2023, 06:24
by swagfag
idk about it being the obvious solution, but is one of possible solutions, the others being:
  • trying to deref the class's name back to a class object(with the caveat that whether doing that is possible would depend on the surrounding context in which the code is executed)

    Code: Select all

    this.msPeriod := %this.__Class%.msDefaultPeriod
  • store some circular references

    Code: Select all

    class cBase
    {
    	static __New() {
    		this.Prototype.DefineProp('MyCustomClassProperty', { Get: (*) => this })
    	}
    ...
    class cInherited extends cBase
    {
    	static msDefaultPeriod := 1000
    
    	__New(Owner, objectName, processName)
    	{
    		super.__New(Owner, objectName)
    		; Set up a hover timer, to check for hovering.
    		this.msPeriod := this.MyCustomClassProperty.msDefaultPeriod
wpb wrote:
29 Mar 2023, 02:44
If I were to change the name of the class, I'd have to change all references to its internal static variables.
if u were to change the name of the class, ud have to change all references period. so, moot point

Re: This value of type "class" has no property named "blah blah"

Posted: 29 Mar 2023, 08:12
by kczx3
I find PHP's Late Static Binding to be quite useful. Perhaps some day AutoHotkey will have such a feature.

Re: This value of type "class" has no property named "blah blah"

Posted: 30 Mar 2023, 15:20
by wpb
Thanks for the extra thoughts on this. %this.__Class% probably would work in my situation - cheers.