Cant Change Key Value **OOP Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
murataygun
Posts: 104
Joined: 07 Aug 2015, 15:53

Cant Change Key Value **OOP

03 Sep 2021, 09:44

Why cant i set the "HaveToBeGlobal " anything else??

Code: Select all

Father := new Father()

class Father {

    __New() {
        childFirst := new Father.childFirst
        childSecond := new Father.childSecond
    }

    class childFirst {

        static HaveToBeGlobal := "my global and changable variable"

        __New() {
            ;I cant change the key value
            this.HaveToBeGlobal := "my global and changable variable 222222222222222"
        }
        
    }

    class childSecond {

        ; i can't set value here.
        __new() {
            msgbox % Father.childFirst.HaveToBeGlobal
        }
        
    }

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

Re: Cant Change Key Value **OOP  Topic is solved

03 Sep 2021, 09:52

for reasons explained here https://www.autohotkey.com/docs/Objects.htm#Custom_Classes_staticvar
To assign to a class variable, always specify the class object; for example, ClassName.ClassVar := Value. If an object x is derived from ClassName and x itself does not contain the key "ClassVar", x.ClassVar may also be used to dynamically retrieve the value of ClassName.ClassVar. However, x.ClassVar := y would store the value in x, not in ClassName.

also Father := new Father() is asking for trouble, see https://www.autohotkey.com/docs/Objects.htm#Custom_Classes
Because the class is referenced via a variable, the class name cannot be used to both reference the class and create a separate variable (such as to hold an instance of the class) in the same context. For example, box := new Box would replace the class object in Box with an instance of itself. [v1.1.27+]: #Warn ClassOverwrite enables a warning to be shown at load time for each attempt to overwrite a class.
murataygun
Posts: 104
Joined: 07 Aug 2015, 15:53

Re: Cant Change Key Value **OOP

03 Sep 2021, 09:58

So this worked;
Is this kind of structure ok to use?

Code: Select all

    class childFirst {

        static HaveToBeGlobal := "my global and changable variable"

        __New() {
            ;I cant change the key value
            Father.childFirst.HaveToBeGlobal := "my global and changable variable 222222222222222"
        }
        
    }

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 339 guests