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 

Passing an array with ByRef to a method does not work!

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
pacodelucia



Joined: 28 Nov 2006
Posts: 3

PostPosted: Tue Feb 12, 2008 3:40 pm    Post subject: Passing an array with ByRef to a method does not work! Reply with quote

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
View user's profile Send private message
TheIrishThug



Joined: 19 Mar 2006
Posts: 381

PostPosted: Tue Feb 12, 2008 4:08 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address
pacodelucia



Joined: 28 Nov 2006
Posts: 3

PostPosted: Wed Feb 13, 2008 1:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
dmatch



Joined: 15 Oct 2007
Posts: 113

PostPosted: Wed Feb 13, 2008 3:37 pm    Post subject: Reply with quote

Hi pacodelucia,

Code:
Demo(S:="Array")
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
View user's profile Send private message
Lexikos



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

PostPosted: Thu Feb 14, 2008 6:07 am    Post subject: Reply with quote

dmatch wrote:
Since Demo expects a by reference (adddress of) variable
No it doesn't... Confused
Code:
Demo(ArrayName)
Back to top
View user's profile Send private message
dmatch



Joined: 15 Oct 2007
Posts: 113

PostPosted: Thu Feb 14, 2008 3:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



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

PostPosted: Thu Feb 14, 2008 11:13 pm    Post subject: Reply with quote

This:
Code:
Demo(S:="Array")
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
View user's profile Send private message
dmatch



Joined: 15 Oct 2007
Posts: 113

PostPosted: Fri Feb 15, 2008 2:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



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

PostPosted: Fri Feb 15, 2008 11:23 pm    Post subject: Reply with quote

dmatch wrote:
It wasn't my example it was TheIrsihThug's example that first contained the Demo(S:="Array").
My mistake. Embarassed Still, my point was valid, and the misinformation from your post has been corrected, so I'm satisfied. Razz

pacodelucia, in case the argument hasn't made it clear, the S := in TheIrishThug's example is unnecessary, though harmless.
Back to top
View user's profile Send private message
dmatch



Joined: 15 Oct 2007
Posts: 113

PostPosted: Sat Feb 16, 2008 12:12 am    Post subject: Reply with quote

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
View user's profile Send private message
pacodelucia



Joined: 28 Nov 2006
Posts: 3

PostPosted: Thu Feb 21, 2008 11:25 am    Post subject: Thank you a lot for your explanations:-) Reply with quote

Thank you a lot for your explanations:-)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help 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