OOP - Calling methods from inside a class Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

OOP - Calling methods from inside a class

09 Mar 2021, 12:50

Code: Select all

class Test {
    foo() {
        msgbox "foobar"
    }

    bar() {
        this.foo()
    }
}


F1::
    test = new Test()
    test.bar() ; nothing happens
    return
How do you access another method from inside a class? This should be simple, but I just can't get it to work.
Are there any detailed and user-friendly documentation/tutorials of how OOP works in AHK? Read a few, but I still can't figure it out.
Or even better, are there any good examples of complex OOP-style AHK scripts in the wild, so that I can study them?
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

Re: OOP - Calling methods from inside a class

09 Mar 2021, 12:59

Oh wow can't believe I didn't notice that, thanks. Now I feel stupid :x .
mcl
Posts: 361
Joined: 04 May 2018, 16:35

Re: OOP - Calling methods from inside a class

09 Mar 2021, 13:02

I also want to point out that since AHK variables aren't case-sensitive, you are overwriting a class with an instance of the same class. It's not a big deal in this script, however.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: OOP - Calling methods from inside a class

09 Mar 2021, 13:04

Code: Select all

    test = new Test()
2 things wrong with this line:
  1. uve assigned the string literal "new Test()" to the variable test using legacy-assignment/traditional-assignment(dont use legacy stuff, use the expression-assignment operator instead :=)
  2. once uve fixed 1., ull want to switch #Warn on(or at the very least #Warn ClassOverwrite) and heed what it's going to tell u
learning OOP is fools errand with how many ahk-specific quirks and gotchas lie in wait. ud be better off learning the fundamentals in a different, stricter language, then coming back and applying them following the same limitations only now theyre gonna be self-imposed ones. then when u slowly get to know ahk's intricacies, can u start employing them but by that point what ud be writing would no longer have much to do with OOP
User avatar
boiler
Posts: 17296
Joined: 21 Dec 2014, 02:44

Re: OOP - Calling methods from inside a class

09 Mar 2021, 13:15

mcl wrote:
09 Mar 2021, 13:02
I also want to point out that since AHK variables aren't case-sensitive, you are overwriting a class with an instance of the same class. It's not a big deal in this script, however.
It didn’t really overwrite the class as demonstrated below (or at least you can still create new instances from it), but it’s a good point about non-case sensitivity in general.

Code: Select all

class Test {
    foo() {
        msgbox "foobar"
    }

    bar() {
        this.foo()
    }
}


F1::
    test := new Test()
	newTest := new Test()
    newTest.bar()
    return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GEOVAN, peter_ahk and 164 guests