Class and local variable of same name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Class and local variable of same name

13 Sep 2017, 06:25

I just spent 4 hours chasing after a ahk Bug because I used a local variable the same name as a Class, in 2 different scripts that call each other, which had the side effect of turning them 2 local variables into a global... and nuking the class

Simplified here is a short example

Code: Select all

Class Something
{
    SomeFunc()
    {
        MsgBox, Something
    }
}
Func1()
{
    something := 10
}
Func2()
{
    something :=1
    Func1()
    Something.SomeFunc() ;gone
    return something
}
F1::
t := Func2() ; will return 10 instead of 1
msgbox,%t%
return
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Class and local variable of same name

13 Sep 2017, 06:55

That's why you should give your classes meaningful names
Recommends AHK Studio
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Class and local variable of same name

13 Sep 2017, 07:19

I'll be more careful and go Old school with the c prefix : CMyClass
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Class and local variable of same name

13 Sep 2017, 12:00

That is why you should declare variables as local ;). Although, about 100 % of the shared around here code doesn't, issues seems to be mostly limited to people missing this in the docs,
classes wrote:When the script is loaded, this constructs an object and stores it in the global (or in v1.1.05+, super-global) variable ClassName.
source

Also, if you use v2. you will get an error when you try to use the class after you have overwritten the reference in the variable className. Eg,

Code: Select all

test:=""
test.f() ; Error on v2, v1 is silent
class test{
	f(){
	}
}
This is of course very convenient and you will save about 4 hours of scratching your head :)

Cheers.
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Class and local variable of same name

15 Sep 2017, 23:40

indeed
when is v2 coming out and will there me a v1 -> exporter? I'm a little worried doing the stuff I'm doing in v1

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750, mikeyww, nacken012 and 237 guests