Passing by reference

This forum is meant for questions and discussions about the X# language and tools
Post Reply
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Passing by reference

Post by boonnam »

Using VO dialect, I notice that passing by reference using @ isn't working. It compile, but the value is not correct.

Code: Select all

 method Method1()
    local iRow as DWORD
    
    iRow := 99
    self:method2(@iRow)
    ? iRow
    
    return NIL
    
 method method2(iRow)
    ? iRow
    
    iRow := 5
    
    return nil
After calling method2, I expect iRow to be 5, but it's 99. But if I call method2(ref iRow), then everything works. Is there a setting I need to set for @ to work like VO? Thanks!
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Passing by reference

Post by Chris »

Hi Boonnam,

You are right, this is supposed to work (when you use the /vo7 compiler option), but indeed it doesn't. And from what I see, this is not a new problem, the same issue exists at least also in the 2.4 build.

We will look into it and fix this, but until then, you can use the REF keyword instead of the @ operator, and this will always work:

SELF:method2(REF iRow)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Passing by reference

Post by boonnam »

Chris,

What about passing array? Is it currently passing by reference as default?
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Passing by reference

Post by Chris »

Hi Boonnam,

Can you please give me a sample of your code and when you expect it to pass by reference? Arrays are different than numeric types in the sense that it is always a reference of them that is passed to methods, but that's a different concept to being passed "by" reference.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Passing by reference

Post by boonnam »

That is exactly what I want to hear about array. No issue. Thanks!
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Passing by reference

Post by Chris »

Hi Boonam,

Just to be clear about passing a reference and passing "by reference":

In the first call below, the called function receives the array and creates a local var pointing to the same array. So when it changes the elements of the array, the changes are reflected back in the original array in the caller code. But if you complete create a completely new array in the called code, this will not be updated in the caller.

In the second call, the REF keyword is used to call the function. In this case, the 2nd array is passed "by reference", which means that you can assign a completely new array to it in the called code and this exact new array will be received back in the caller code.

In one of the next builds, you will be able to do the same thing (pass anything by reference using the @ operator, same as you can do with REF now). But usually that's not what you generally want to do with arrays, what you want to do is to be able to update the elements of the array, not the array itself, and this works already.

Code: Select all

FUNCTION Start() AS VOID
LOCAL a,b AS ARRAY

a := {"old 1","old2"}
b := {"old 1","old2"}
TestArray(a,@b)
? a[1] // changed same array 1
? b[1] // old 1

?

a := {"old 1","old2"}
b := {"old 1","old2"}
TestArray(a,REF b)
? a[1] // changed same array 1
? b[1] // created new array 1

PROCEDURE TestArray(a,b)
a[1] := "changed same array 1"
b := {"created new array 1"}
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply