On mardi 02 septembre 2008, [EMAIL PROTECTED] wrote: > Am Dienstag, 2. September 2008 12:06:09 schrieb Emil Tchekov: > > Forgot to gave you example (VB) > > > > > > sub main() > > > > dim a as integer > > > > a=1 > > > > debug.print a, pr1(a) > > debug.print a, pr2(a) > > debug.print a > > > > end sub > > > > > > private sub pr1(byval a) > > > > a=2 > > > > return a > > > > end sub > > > > > > private sub pr2(byref a) > > > > a=2 > > > > return a > > > > end sub > > > > > > result will be as follows > > > > 1 2 > > > > 1* 2 > > > > 2 (!) > > > > > > *As you can see on the first sub the var a is local to your sub, it was > > changed (returns 2), but the passed a has stil value 1 > > > > (!) in the second procedure the passed var a was changed! (the a that was > > defined in the sub main!) > > Hi Emil, > > thanks for your reply! ...it helped to understand the concepts and the > syntax. Meanwhile I got it running. Gambas wants the "Byref" twice! Once by > calling the procedure and a second time in the header of the function. > > Sub Main > > DIM sResult as String > sResult = "" > GetData(Byref sResult) > Print sResult & "\n" > End > > > Public Sub GetData(ByRef sResult As String) > > sResult = "Hello" > > End > > Greetz > Stevie >
The ByRef feature was mainly added to help people porting VB project. It does not work by passing pointers, but by keeping the value of the argument when the function ends, and put it into the expression passed ByRef. GetData(ByRef sResult) does actually the following: GetData(sResult) ' Push sResult on the stack as first argument. ... ' Do not free the stack after GetData ends. sResult = ... ' Gets the value from the stack and put it into sResult. Note that this way, any assignment expression can be passed by reference. GetData(ByRef MyCollectionOfLabels["key"].Text) At function declaration, ByRef means that you *can* pass the argument by reference, but that you may not. At function call, ByRef means that you *want* the argument to be passe by reference. As Gambas linking is entirely dynamic, the interpreter checks at runtime that you use ByRef if the function really allows it. This is the reason why ByRef must be specified noth at function declaration and at function call. Moreover, you can see that ByRef makes the function call slower because of the needed checks and the recall of the value from the stack. Regards, -- Benoit Minisini ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user