AutoHotkey Community

It is currently May 27th, 2012, 1:05 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: December 31st, 2011, 8:04 pm 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
Hi everyone.
Happy Holidays and End of Year/New Year to all.
I'm new to objects, associative arrays, class and so on.
I know I can pass an associative array as a ByRef parameter to a function and get back the value of a key modified by the function (Param3 in my example).
But I'd like to pass back, in a key, an other associative array (whose number of keys - structure - is not predetermined and so can't be set before the call to the function but is determined inside the function itself) and I'd like to get the value associated to one of its keys.
So in my example, I'd like to transmit back, in the "ParamFour" key ot the associative array named "Lib_Func_Params", an associative array named "TheFunc_Params" and then access to one of the associated value of one of its keys, so fo example to get back the value "coucoulina" of the key "Par5" of the "TheFunc_Params" associative array, returned in the "ParamFour" key of the "Lib_Func_Params" associative array, passed as a ByRef parameter to the "TheFunc" funcion !... Whew!
Is it possible and, if it is, how ?
Thanks for your answers.

Code:
Lib_Func_Params := {ParamOne: 0, ParamTwo: "Value B", ParamThree: 24, ParamFour: "", ParamFive: "goubiboulga"}

x := TheFunc(Lib_Func_Params)

Param3 := Lib_Func_Params["ParamThree"]
Param4 := Lib_Func_Params["ParamFour"],TheFunc_Params["Par5"]

msgbox Param Three  "%Param3%"
msgbox Param Four  "%Param4%"

TheFunc(ByRef ParamArray)
{
   TheFunc_Params := {Par1: 0, Par2: "Value C", Par3: 24, Par4: "", Par5: "coucoulina"}
   
   ParamArray["ParamThree"] := 25
   ParamArray["ParamFour"] := TheFunc_Params
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2011, 11:32 pm 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
I found the answer to my question.
I want to thank me warmly (and Lexicos !!!).
At first, without initialising it (by giving values to the keys), I declare the associative array with a "Global" instruction outside the function to make it available everywhere, as it is now a Super Global variable (thanks Lexicos). If I would not made that, the array named "TheFunc_Params" would be only available inside the function. Once we go back to the caller, the array would no longer exists, as it would be local to the function, so we can't get back any value.
The array is declared Super Global but the values are affected to the keys inside the function, and I can have as many keys that I want to return the appropriate number of values to the caller. That's what I wanted...

The second modification I made is to use multi-dimensionnal associative array, with the correct syntax (see Param2 and Param4)...

So now, I can return as many values that I want in an associative array that is associated with one of the keys of the associative array used as the argument of the function !!!

May be it sounds complicated, but it is a very flexible solution. You can use an associative array as the sole argument of a function, which can pass as many parameters as you want to the function, and the same for the return values. And you can even return an array of parameters associated with one of the keys of the associative array of parameters used as the argument of the function, when your function, depending of some circumstances, may return a variable number of results to the caller.

Here is the code :
Code:
Lib_Func_Params := {ParamOne: 0, ParamTwo: "Value B", ParamThree: 24, ParamFor: "", ParamFive: "goubiboulga"}

Global TheFunc_Params

x := TheFunc(Lib_Func_Params, VariaParam*)

Param3 := Lib_Func_Params["ParamThree"]

Param2 := Lib_Func_Params["ParamFor","Par2"]
Param4 := Lib_Func_Params["ParamFor"]["Par5"]

msgbox Param Three  "%Param3%"
msgbox Param Four  "%Param4%"
msgbox Param Two  "%Param2%"

TheFunc(ByRef ParamArray, variaParam*)
{
   ParamArray["ParamThree"] := 25
   TheFunc_Params := {Par1: 0, Par2: "Value C", Par3: 24, Par4: "", Par5: "coucoulina"}
   ParamArray["ParamFor"] := TheFunc_Params
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2012, 12:51 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
ByRef allows you to assign a new object or some other value to the variable. In this case, ByRef makes "ParamArray" an alias for Lib_Func_Params. Without ByRef, ParamArray and Lib_Func_Params would be separate variables which initially contain references to the same object. Either way any changes you make to the object inside the function can be seen outside the function.

Recommendation: Remove ByRef.

Global variables should generally be avoided when there are good alternatives. If you need to return multiple values, you can simply return an object (which in your case would be stored in the variable x). You could even return VariaParam if you wanted to. Alternatively, you can store values in an object which the caller passes a reference to. Since that's what you've already done in your second post, it works perfectly fine even without the global declaration.

Recommendation: Simply remove the global declaration.

Quote:
If I would not made that, the array named "TheFunc_Params" would be only available inside the function. Once we go back to the caller, the array would no longer exists, as it would be local to the function, so we can't get back any value.

This is not true. An object (or array) will remain in memory as long as at least one reference to it exists. Note that there is no array named "TheFunc_Params"; there is a variable named "TheFunc_Params" which contains a reference to an array, which is a separate entity.
Quote:
An object reference is a pointer or "handle" to a particular object. Like strings and numbers, object references can be stored in variables, passed to or returned from functions and stored in objects. After copying a reference from one variable to another as in x := y, both variables refer to the same object.
Source: Objects


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2012, 6:48 am 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
Thanks a lot for your informations and recommandations, Lexicos. I did what you recommended to do and (of course !) it works perfectly. I have to more study Objects and so on. Here is a good resolution for 2012... Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2012, 9:19 am 
Offline

Joined: October 11th, 2010, 12:30 pm
Posts: 406
Thanks Nemroth, very illustrative example


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Amandaville, BrandonHotkey, chaosad, Yahoo [Bot] and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group