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 

1.0.47.06 Array references when Gui is within a function

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



Joined: 03 May 2007
Posts: 16

PostPosted: Sun Mar 30, 2008 8:37 am    Post subject: 1.0.47.06 Array references when Gui is within a function Reply with quote

The following two code examples show the same gui with the exception that the second version is wrapped in a function.

In each instance I've created a simple two element array with items fArray1 = "one", fArray2 = "two" and fArray0 = 2

In the first piece of code the variables are global. In the second instance the variables are local to the function.

The first piece of code runs as expected. The array can be referenced as fArray%A_Index% outside of the gui subroutines and within the gui subroutines. That's what I expected.

The second piece of code can reference the array as fArray%A_Index% *only* while not doing so within a subroutine of the gui.

Inside a gui subroutine within a function, fArray1 = "one" and fArray2 = "two, but fArray%A_Index% is blank even though A_Index = 1 or A_Index = 2. It doesn't appear to be a problem referencing the variables themselves since I can force the value by avoiding %A_Index% style referencing and using a static fArray1 or fArray2.

It just seems to be related to referencing the values of the array via a %A_Index% suffix within a loop of a subroutine of the gui.

Code:
fArray0 := 2
fArray1 := "one"
fArray2 := "two"
Loop, %fArray0%
  MsgBox % "fArray%A_Index% = [" fArray%A_Index% . "] when A_Index = " . A_Index

Gui, 65:Destroy
Gui, 65:Add, Button, w75 g65Test, Test
Gui, 65:-MinimizeBox
Gui, 65:-MaximizeBox
Gui, 65:Show,, Odd Demo
Gui, 65:+LastFound
WinWaitClose, Odd Demo
Gui, 65:Destroy
ExitApp

65Test:
Loop, %fArray0%
  MsgBox % "fArray%A_Index% = [" fArray%A_Index% . "] when A_Index = " . A_Index
MsgBox % "while fArray1 and fArray2 = " . fArray1 . " & " . fArray2
Return


Code:
fGuiFunction()
ExitApp


fGuiFunction()
{
  fArray0 := 2
  fArray1 := "one"
  fArray2 := "two"
  Loop, %fArray0%
    MsgBox % "fArray%A_Index% = [" fArray%A_Index% . "] when A_Index = " . A_Index

  Gui, 65:Destroy
  Gui, 65:Add, Button, w75 g65Test, Test
  Gui, 65:-MinimizeBox
  Gui, 65:-MaximizeBox
  Gui, 65:Show,, Odd Demo
  Gui, 65:+LastFound
  WinWaitClose, Odd Demo
  Gui, 65:Destroy
  Return

  65Close:
  Gui, 65:Destroy
  Return

  65Test:
  Loop, %fArray0%
    MsgBox % "fArray%A_Index% = [" fArray%A_Index% . "] when A_Index = " . A_Index
  MsgBox % "while fArray1 and fArray2 = " . fArray1 . " & " . fArray2
  Return
}
Back to top
View user's profile Send private message
jballi



Joined: 01 Oct 2005
Posts: 297
Location: Texas, USA

PostPosted: Sun Mar 30, 2008 6:46 pm    Post subject: Reply with quote

I've already banged my head on the wall with this one. The problem is that independent threads (when you press the button, an independent thread is fired) do not like dynamic variables unless they are global.

See this thread for more information (Thanks to Lexikos for identifying the problem):
http://www.autohotkey.com/forum/viewtopic.php?t=25258
Note:The 3rd post is my summary of the problem.

The work around in your second script is to define your array as global.

I hope this is helpful...
Back to top
View user's profile Send private message
LBJ



Joined: 03 May 2007
Posts: 16

PostPosted: Sun Mar 30, 2008 11:24 pm    Post subject: Reply with quote

jballi wrote:
The work around in your second script is to define your array as global.

I hope this is helpful...


Thanks jballi. That's what I ended up doing, and yes, you were helpful.

I did a bit of head banging myself, so it's nice to know it's not just me. Smile
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