PRB: Cannot Pass an Array Member to a ProcedureLast reviewed: April 30, 1996Article ID: Q130224 |
The information in this article applies to:
SYMPTOMSAn array that is declared as a member of a class cannot be passed by reference to a function or a method. For example, if you declare an array as a property of a form, the error "Alias 'this' is not found" is displayed when a method is called as follows:
This.pNewFunction(@this.atest) CAUSEVisual FoxPro always passes object properties by value. Object properties cannot be passed by reference.
WORKAROUNDAn alternative is to pass the array name to a method, and manipulate the array or a copy of the array. The following example code defines a form with a command button. When the command button is clicked, a function is called that adds 10 to each element of an array declared as a property of the command button. The same method can be used in the Form Designer.
Sample CodePUBLIC oform oform=CREATEOBJECT('frmtest') oform.Show DEFINE CLASS frmtest AS FORM ADD OBJECT cmd1 AS cmdENDDEFINE DEFINE CLASS cmd AS COMMANDBUTTON DIMENSION aprop[10] PROCEDURE init LOCAL ii
FOR ii = 1 TO 10
This.aprop[ii]=ii
ENDFOR
ENDPROC
PROCEDURE click LOCAL ii
THIS.ADDEM(This, 'aprop')
ACTIVATE SCREEN
FOR ii =1 TO 10
?This.aprop[ii]
ENDFOR
ENDPROC
* Two parameters are passed to the procedure: a reference to the object, * and the array name. PROCEDURE ADDEM LPARAMETERS oref, cprop
local ii, atemp[1]
=ACOPY(oref.&cprop,atemp) & Makes a copy of the member array.
FOR ii = 1 TO aLEN(oref.&cprop)
atemp[ii]=atemp[ii] + 10
ENDFOR
=ACOPY(atemp,oref.&cprop)
ENDPROC
ENDDEFINE
STATUSThis behavior is by design.
|
Additional reference words: 3.00 VFoxWin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |