PtrPersistent

Creates a "pointer" to a persistent variable.

Syntax:

PtrPersistent ( variable-name, value )

Parameters:

(s) variable-name: specifies a variable name which CANNOT be longer than 25 characters.

(s/i) value: specifies a value which will be assigned to "variable-name" if the variable does not already exist.

Returns:

(s) pointer a pointer string.

 

This function marks a variable as persistent. If called from a UDF, the variable will retain its value after the UDF returns.

PtrPersistent initializes the variable the first time a UDF calls the function even if the variable is already instantiated with a value.  This means you cannot assign a value to the variable before the first call to PtrPersistent and expect the value to be preserved.

A "persistent" variable is defined and used in a single UDF. But unlike other UDF variables, it "stays around". Kind of like a private global variable that no other UDF's can see. For example, if a UDF wants to save away some information that it needs, it can use a persistent variable. The next time the UDF is called, its persistent variable is still there, with what ever value was left over from the previous call to it.'

 

Example:


#DefineFunction IncMyCounter(flag)
; flag = 0 will report on current value of counter
; flag = 1 will increment counter first
Ptr_MyCounter = PtrPersistent(MyCounter,0)
If flag==1 ; report on number of calls
   *Ptr_MyCounter = *Ptr_MyCounter + 1
EndIf
Return *Ptr_Mycounter
#EndFunction
r=Random(100)
For xx = 1 To r
   IncMyCounter(1)
Next
cnt=IncMyCounter(0)
Message("Counter Value is",cnt)
See Also:

PtrGlobal, PtrGlobalDefine