Passare un argomento per riferimento

La funzione BYREF è stata aggiunta principalmente per aiutare le persone a trasferire un progetto VB.

Non funziona attraverso il passaggio di puntatori, ma non rilasciando il valore dell'argomento quando termina la funzione e inserendolo nell'espressione passata per riferimento.

In altre parole:

GetData(ByRef sResult)

attua quanto segue:

GetData(sResult) ' Spingere sResult sullo stack come primo argomento.
...              ' Non libera lo stack dopo la fine di GetData.
sResult = ...    ' Ottiene il valore dallo stack e lo inserisce in sResult.

Nota che in questo modo, qualsiasi espressione di assegnazione può essere passata per riferimento.

GetData(ByRef MyCollectionOfLabels["key"].Text)

Quando dichiari una funzione ByRef, significa che tu puoi passare un argomento per riferimento, ma non sei obbligato.

Quando invece chiami la funzione ByRef, significa che vuoi che l'argomento sia passato per riferimento.

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 both at function declaration and at function call.

ByRef makes the function call slower because of the needed checks and the added process of recalling the value from the stack.