I'm trying to understand how static class properties work in AutoHotkey and see if I can use them in my scripts.
I expected the first line in the message box in the following code shows the changed value. But it doesn't.
Code: Select all
_o1 := new StaticProperties
_o1.sStaticVar := "changed"
_o1.sVar := "changed"
_o2 := new StaticProperties
msgbox % "1: " _o2.sStaticVar "`n" ; <-- this does not show `changed`
. "2: " _o2.sVar
class StaticProperties {
static sStaticVar := "this is static"
sVar := "tihs is NOT static"
}
But I'm surprized that changing the value of properties having the same name declared as static from an instantiated object does not actually modify their static values. So they seem to be entirely different entities although they share the same name.
Also modifying them from an extended class isn't possible using the `this` and `base` keywords.
Code: Select all
_o2 := new StaticProperties2 ; should update the static property in the consturctor.
_o1 := new StaticProperties
msgbox % _o1.sStaticVar ; <-- this does not show `updated`
class StaticProperties {
static sStaticVar := "this is static"
}
class StaticProperties2 extends StaticProperties {
__New() {
this.base.sStaticVar := "updated" ; doesn't update the base static value
base.sStaticVar := "updated" ; doesn't update the base static value
this.sStaticVar := "updated" ; doesn't update the base static value
; StaticProperties.sStaticVar := "updated" ; this does
; msgbox % this.base.sStaticVar ; shows `updated`
; msgbox % base.sStaticVar ; shows `this is static`
}
}
So when do you use static properties? I thought I could use them for caching values shared among derived objects but at the moment, I find it very difficult to manage.