AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with GUI Global Variables

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
Icarus



Joined: 24 Nov 2005
Posts: 507

PostPosted: Sun Sep 28, 2008 9:19 am    Post subject: Problem with GUI Global Variables Reply with quote

I am not 100% sure this is a bug, but it may be.

When making a function that then calls a label that attempts to build a GUI with variables for controls, I get an error that variables must be global, even though they are (since they are inside a label and not a function).

Here is a reproduction code:
Code:

#SingleInstance Force
#NoEnv
#NoTrayIcon

Test()
msgbox %TestVar%

Return

Test() {
  ;Global
  Gosub BuildGui
}

BuildGui:
  ; This global variable works and will be displayed in the msg box above
  TestVar := "hello"
 
  ; This line will only work if "Global" decleration is added to Test()
  ; Otherwise it generates an error "Gui Variables must be global" 
  ; Uncomment the line below to get an error.
  ;Gui Add, Edit, w100 vGuiVar, hello world
  ;Gui Show
Return

ESC::ExitApp

_________________
Sector-Seven (Music and Utilities)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Lexikos



Joined: 17 Oct 2006
Posts: 2737
Location: Australia, Qld

PostPosted: Sun Sep 28, 2008 9:47 am    Post subject: Reply with quote

Although the subroutine is not defined inside a function, when it is called, a function is executing, so the variable is created as a local. If you refer to the variable outside of any functions at least once (which you'd no doubt need to do at some point), the variable is created as global at load-time, and the Gui command succeeds.

I think it would be more appropriate for the subroutine to execute in the global context, rather than the context of the function. This adds some complexity, since AutoHotkey must take note of where the subroutine was defined when executing it.
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 507

PostPosted: Sun Sep 28, 2008 10:13 am    Post subject: Reply with quote

So TestVar was working just because it was called once outside of a function.... thats confusing. Smile

I did not know that labels inherit the scope of their caller. I was under the assumption that labels are global all the time.
_________________
Sector-Seven (Music and Utilities)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Lexikos



Joined: 17 Oct 2006
Posts: 2737
Location: Australia, Qld

PostPosted: Sun Sep 28, 2008 12:43 pm    Post subject: Reply with quote

Quote:
I did not know that labels inherit the scope of their caller.
I prefer to think of a label as a simple marker in the code, and a subroutine as a concept/one use of a label. AutoHotkey is not aware of a subroutine until it is actually running, so labels (or subroutines) do not affect variable scope.

More specifically, entering a function sets g.CurrentFunc. Since gosub does not affect g.CurrentFunc, execution is still "in the function."
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group