#72996 - 2003-02-09 05:59 AM
Passing Var by Reference (sorta)
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
New Mexico Mark posted some interesting code in the ScheduleTask UDF thread.. I tried the concept in a new project and it's pretty cool. I thought I'd post an example here so it isn't buried in post 27 or so.
Basically, it allows you to pass the NAME of a variable to a function. The function can then convert the name to a variable and make a local copy. By adding a line of code that reverses the process, when the function exits, it copies its local copy back to the original.
Not quite as elegant as indirection, but useful nonetheless.
Glenn
code:
; example - red fish in, blue fish out ; Define simple array $Seuss = "Red", "Fish" ; Display contents "Seuss before fn: " + $Seuss[0] + " " + $Seuss[1] ? ; call the function, passing the NAME of the var ; note simple text string, no leading "$" fn("Seuss") ; display contents after return from function "Seuss after fn: " + $Seuss[0] + " " + $Seuss[1] ? Function fn($ArgName) ; $ArgName contains the name of an external variable ; convert the arg name to an arg reference ("var" becomes "$var") ; remember that "$$" in a string becomes a single "$" when evaluated ; thus, the Execute "sees" ($Arg = $Seuss) $RC = Execute('$$Arg="$$"+$ArgName') ; copy the received var to a local var $RC = Execute('$$fArg=$Arg') ; display the contents of the local variable "fn received: " + $fArg[0] + " " + $fArg[1] ? ; modify the local variable $fArg[0] = "Blue" ; show the contents of the modified local var "modified, fn contains: " + $fArg[0] + " " + $fArg[1] ? ; copy local var back to original $RC = Execute('$Arg=$$fArg') EndFunction
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#72997 - 2003-02-09 02:07 PM
Re: Passing Var by Reference (sorta)
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Take a look at my HASH UDFs for some other code along these lines. [ 09. February 2003, 14:09: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
#73000 - 2003-02-09 04:26 PM
Re: Passing Var by Reference (sorta)
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Unfortunately Jens the GLOBAL variable or array used inside a UDF is required until we can pass by reference.
|
|
Top
|
|
|
|
#73001 - 2003-02-09 06:20 PM
Re: Passing Var by Reference (sorta)
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Global? Maybe I'm not understanding your references.. I haven't defined anything as global, and the point of the example is to show that I can define a variable (regular or array) in the main program, pass it's name to a function, and have the function modify it. Granted, without a lot of Execute() code, it relies on making a copy for internal use and then copying it back to the original, but...
BTW, Howard - the caffiene has begun to flow. It IS what I thought, and an excellent (and USEFUL) example of using the Execute code. I'll be using it in the future, thanks! Oh, one thing - it's "associati-V-e" - a minor typo.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#73002 - 2003-02-09 06:27 PM
Re: Passing Var by Reference (sorta)
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Corrected that typo a couple hours ago in the KiXtart.org UDF forum. The mirror sites will get updated shortly.
|
|
Top
|
|
|
|
#73005 - 2003-02-10 01:12 AM
Re: Passing Var by Reference (sorta)
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Glenn, did you consume sufficient amounts of caffine to work your way throught the HASH UDFs yet?
|
|
Top
|
|
|
|
#73006 - 2003-02-10 10:26 AM
Re: Passing Var by Reference (sorta)
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Glenn, Howard is making the point that the "$Seuss" variable is global - this is the default scope for variables unless you "Dim" them.
If you "Dim" your $Seuss variable so that it is only visible in the local scope you will find that your function no longer works. This is also a problem for files included with "CALL".
That doesn't mean that using Execute() to evaluate variable names isn't any less useful, sometimes it is the only way to construct a UDF (see the example push/pop functions here)
You just need to be aware that the variables must have a global scope to manipulate them this way.
Until we have static variables and pass-by-reference we won't be able to create truly discreet functions. [ 10. February 2003, 10:29: Message edited by: Richard H. ]
|
|
Top
|
|
|
|
#73007 - 2003-02-10 02:09 PM
Re: Passing Var by Reference (sorta)
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
fnAllSpecialFolders() may provide another example for you.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|