For Programmers: Free Programming Magazines  


Home > Archive > Clipper > August 2006 > private variable ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author private variable ?
fatfat

2006-08-02, 7:55 am

is there any internal function to assign private variable that belongs to
the calling function instead of running function ?

Thanks


LHSoft@gmail.com

2006-08-02, 7:55 am

Fatfat,

The scope of PRIVATE variables are that they can be seen from the
function they are defined in and in all functions/procedures you call
from there.

If you want the var to be seen only in the function it's defined in,
use LOCAL variables.

Example

Function Test1(someparameter)
LOCAL var1 := 2, var2 := 2, retval 1 := 1, retval2 := 1
PRIVATE pVar1 := 3, pVar2 := 3

retval1 := Test2() // will return 9, because func sees those
variables

retval1 := Test3() // error var1 and var2 can't be seen

retval2 := Test4(var1, var2) // the values beeing sent to the
function

Return(.t.)



Function Test2()
Return(pVar1 * pVar2)

Function Test3()
Return(var1 * var2)

Function Test4(x,y) // x and y receives the values from the callinf
function but are local to this function
Return(x * y)


Lasse


fatfat wrote:
> is there any internal function to assign private variable that belongs to
> the calling function instead of running function ?
>
> Thanks


Robert Haley

2006-08-03, 9:55 pm

Greetings fatfat,

It depends on what you are trying to accomplish.

If you declared a PRIVATE variable in the calling function, and have
declared a LOCAL or STATIC variable of the same name as the (PRIVATE)
variable in the called function, the LOCAL or STATIC variable will take
precedence over the (PRIVATE) variable, in short the (PRIVATE) variable
will be (disabled) in the called function. Fix is to ensure the
(PRIVATE) variable name is unique throughout the callstack, e.g. from
the calling function -> to the called function.

If you have declared a PRIVATE variable in the calling function and are
attempting to (modify) the contents of the variable in the called
function, provided the variable is visible to the called function, any
changes to the PRIVATE variable in the called function should be
reflected back to the calling function.

If you have declared a LOCAL or STATIC variable in the calling function
and wish to modify the contents of the variable in the called function,
you need to pass the variable as a parameter to the called function, by
reference:

FUNCTION Main()
LOCAL cName := ""
MyNameIs( @cName )
RETURN NIL

FUNCTION MyNameIs( cName )
cName := "Robert"
RETURN NIL

LOCAL variable cName now contains value "Robert".

--
If you wish to (create) a new variable / value assignment in the called
function & retrieve it's contents in the called function, I recommend
passing an (Array) to the called function, then retrieving the contents
of the Array element, e.g.:

#define FIRST_NAME 1
#define LAST_NAME 2

FUNCTION Main()
LOCAL aArray_ := {}

ASIZE( aArray_, 2)
AFILL( aArray_, "")

MyNameIs( aArray_ )

? aArray_[FIRST_NAME]
? aArray_[LAST_NAME]

RETURN NIL

// Demonstrates that Arrays are passed by reference to called
functions..
// Let's adjust the array contents in called function - the values
assigned
// will be visible above.

FUNCTION MyNameIs( aName_ )

IF LEN( aName_ ) < 2
ASIZE( aName_, 2 )
AFILL( aName_, "" )
ENDIF // bounds checking..

aName_[FIRST_NAME] := "Robert"
aName_[LAST_NAME] := "Haley"

RETURN NIL

---

Passing a LOCAL array by reference to the called function has the
benefit of using a LOCAL variable & utilizing it with the scope of a
PRIVATE variable: you should pass the entire Array (again, by
reference) to any additional called functions, in the same manner as
shown. The use of manifest constants (#define FIRST_NAME 1) allows you
to identify each array element, in the same manner as assigning a
unique name to a variable.

I hope the info helps.

Regards,
Bob


fatfat wrote:
> is there any internal function to assign private variable that belongs to
> the calling function instead of running function ?
>
> Thanks


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com