| View previous topic :: View next topic |
| Author |
Message |
pacodelucia
Joined: 28 Nov 2006 Posts: 3
|
Posted: Tue Feb 12, 2008 3:40 pm Post subject: Passing an array with ByRef to a method does not work! |
|
|
The method Demo(ByRef Array) does not change the passed parameter Array as expected. According to the documentation there is no limitation for arrays!
| Code: |
Array%1%_1 := "mmm"
MsgBox, Out: %Array_1% ;Shows mmm
Demo_D(Array)
MsgBox, Out: %Array_1% ;Shows mmm :-(
Demo(ByRef Array)
{
Array%1%_1 := "paco"
MsgBox, In : %Array_1% ; Shows paco :-)
}
|
[ Moderator!: Moved from Bug Reports ] |
|
| Back to top |
|
 |
TheIrishThug
Joined: 19 Mar 2006 Posts: 381
|
Posted: Tue Feb 12, 2008 4:08 pm Post subject: |
|
|
This is because AutoHotkey doesn't have real arrays. Array is a completely separate variable from Array1.
To assign arrays by reference you have to use the global keyword
| Code: | Array1 := "mmm"
Var1 := "nnn"
MsgBox, Out: %Array1% ;Shows mmm
Demo(S := "Array")
MsgBox, In : %Array1% %Var1% ; Shows paco nnn :-)
Demo(ArrayName)
{
global
local Var1
%ArrayName%1 := "paco"
Var1 := "bbb"
MsgBox, In : %Array1% %Var1% ; Shows paco bbb :-)
} |
|
|
| Back to top |
|
 |
pacodelucia
Joined: 28 Nov 2006 Posts: 3
|
Posted: Wed Feb 13, 2008 1:36 pm Post subject: |
|
|
Thanks for the explanation. My code works fine now. By the way, where can I find more about this notation: S := "Array" ?
I would like to know exactly what it means and how I can use it. |
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Wed Feb 13, 2008 3:37 pm Post subject: |
|
|
Hi pacodelucia,
would have the same functionality as: | Code: | S:="Array" ;puts the string (literally) Array into variable S.
Demo(S) ; Call Demo with variable S as parameter |
Since (Edit:If) Demo expects a by reference (adddress of) variable you have to assign the string "Array" to a variable to pass it to the function.
dmatch
Last edited by dmatch on Sat Feb 16, 2008 12:14 am; edited 2 times in total |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2737 Location: Australia, Qld
|
Posted: Thu Feb 14, 2008 6:07 am Post subject: |
|
|
| dmatch wrote: | | Since Demo expects a by reference (adddress of) variable | No it doesn't...
|
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Thu Feb 14, 2008 3:26 pm Post subject: |
|
|
In first post:
| Code: |
Demo(ByRef Array)
{
Array%1%_1 := "paco"
MsgBox, In : %Array_1% ; Shows paco :-)
}
first post: |
The OP was talking about passing a variable BYREF.
dmatch |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2737 Location: Australia, Qld
|
Posted: Thu Feb 14, 2008 11:13 pm Post subject: |
|
|
This:
was not in the first post. It was in yours (Edit: TheIrishThug's), which does not use ByRef. It is not necessary to assign "Array" to a variable before passing it to Demo(), in your example.
Last edited by Lexikos on Fri Feb 15, 2008 11:19 pm; edited 1 time in total |
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Fri Feb 15, 2008 2:49 pm Post subject: |
|
|
It wasn't my example it was TheIrsihThug's example that first contained the Demo(S:="Array").
Whether it will make you feel better or not I don't know, but I will insert a tiny little "if" in the previous post just in the hope that will stop the quibbling.
dmatch |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2737 Location: Australia, Qld
|
Posted: Fri Feb 15, 2008 11:23 pm Post subject: |
|
|
| dmatch wrote: | | It wasn't my example it was TheIrsihThug's example that first contained the Demo(S:="Array"). | My mistake. Still, my point was valid, and the misinformation from your post has been corrected, so I'm satisfied.
pacodelucia, in case the argument hasn't made it clear, the S := in TheIrishThug's example is unnecessary, though harmless. |
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Sat Feb 16, 2008 12:12 am Post subject: |
|
|
The thing that first popped into my mind when I saw the Demo(S:="Array") was when I first got into AutoHotKey and was seeing what was, to me at the time, cryptic code such as: | Code: | | SomeFunc(A, B, _:="SomeString") | It finally dawned on me that the _ was a variable name and was being assigned a string so that (in at least one case) it could be passed BYREF.
Anyway, that is where my head was and I think the request about "this notation" has been answered.
dmatch |
|
| Back to top |
|
 |
pacodelucia
Joined: 28 Nov 2006 Posts: 3
|
Posted: Thu Feb 21, 2008 11:25 am Post subject: Thank you a lot for your explanations:-) |
|
|
| Thank you a lot for your explanations:-) |
|
| Back to top |
|
 |
|