Can't change variable passed by reference


Please look at this code:

function readLevelsMxLineByIndex( LevelsMx, index, fPrice, fType, fStrength, fDateTime, fInterval, fBarIndex )
    local  tprice, tinterval, rval;

        fPrice = 5;
        fType = 6;
        fStrength = 200;
        fDateTime = SelectedValue(DateTime());
        fInterval = 60;
        fBarIndex = 100;
    return true;

function create_trends_endpoints()
    bi2, i, totalSRLines, trendpoints, QuickAFLBarIndex, 
    pPrice, pTypexx, pStrength, pDateTime,pInterval, pBarIndex;

    totalSRLines = MyMxGetSize( TFSRLineMx, false );
    trendpoints = False;  // initialization
    for( i = 0; i <= totalSRLines - 1; i++ )
        if( readLevelsMxLineByIndex( TFSRLineMx , i, &pPrice, &pTypexx, &pStrength, &pDateTime, &pInterval, &pBarIndex))

            if( pTypexx != TypeRoundNumber )
// AT THIS POINT pTypexx = 10, INSTEAD OF 6    WHY????

pTypexx is set to 10, then passed to function readLevelsMxLineByIndex and is supposed to take the value 6. However it remains 10.

I am sending a screenshot from the debug mode, with a strange message:
fType= reference to ptype(=reference to ptypexx (=10))

I am sure here is the key. Somehow pType comes into play. pType is the old variable name, before I renamed it to fType.

Any idea how to fix this please?

If you want help you need to post WORKING code, not part of it. The code that you want help with must be ready to copy-paste and run without modification.

Hi Tomasz

I am trying to replicate the error with more simple code and I can’t. The original code is more than 2000 lines. I could email it to you as a last resort.

I was hopping that this message would immediately tell you what is going on. “fType= reference to ptype(=reference to ptypexx (=10))”

ok, I managed to replicate it with very simple code:

function myFunc( fType )
    fType = 26;
    return true;

    xx1, xx2;

    xx1 = 5; 
    if( myFunc( &xx1 ) )
        printf( "Should be 26, it is %g\n", xx1 );


    xx1 = 30; 
    xx2 = 10; 
    if( myFunc( &xx2 ) )
        printf( "Should be 26, it is %g\n", xx2 );
        printf( "Should be 30, it is %g\n", xx1 );

This is the output:

Should be 26, it is 26
Should be 26, it is 10
Should be 30, it is 26

From what I see, the second time that myFunc is called, it changes the value of variable xx1, instead of xx2.

As noted in AmiBroker 6.25.0 BETA released thread passing by reference (new feature) is incomplete yet. You need to wait for 6.26.

Hi Tomasz

May I ask when the 6.26 is expected to be released?

This and other things are already implemented in the development version but I’ve got other things that need to be included too.

Anyway the plan is to release this week.

UPDATE: Most likely it will arrive before Friday this week.


6.26 is available now and your code is working now OK.