 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
LBJ
Joined: 03 May 2007 Posts: 16
|
Posted: Sun Mar 30, 2008 8:37 am Post subject: 1.0.47.06 Array references when Gui is within a function |
|
|
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 |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 297 Location: Texas, USA
|
Posted: Sun Mar 30, 2008 6:46 pm Post subject: |
|
|
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 |
|
 |
LBJ
Joined: 03 May 2007 Posts: 16
|
Posted: Sun Mar 30, 2008 11:24 pm Post subject: |
|
|
| 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.  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|