sorry for the delay in posting - things have been a little hectic the past 10 days or so. anyway, here is the next in my little series of random jottings. this time i'm looking at variable scoping - and next week i'll continue with properties and methods. enjoy

scoping in visual foxpro – part 1

one of the issues that seems to cause endless confusion is that of the scope of properties, methods and variables in visual foxpro. in fact it is really quite simple, but can be confusing because of the differences in syntax and meaning. let’s start, this week, with variables and we’ll discuss properties and methods next week.

variable scoping

the scope of a variable defines how long it exists and which programs, methods, procedures, or user defined functions (udfs) have access to it. just for the purpose of discussing scope, let’s think of the term “procedure” as including programs, methods, procedures, and user-defined functions. with this in mind, there are three levels of scope:

public variables

also known as “global” – in fact the recommended scope identifier for a public variable is “g” for this reason (the letter “p” being reserved for “private”).  a public variable is available to an entire application irrespective of where, or when it is created. it remains extant until it is specifically released by name or the application  (or vfp itself) is closed. this means that any program can read, or change, the value of the variable at any time. while there are some, very specialized situations in which a public variable may be the only solution, as a general rule their use in program code is not recommended – they are simply too vulnerable to unexpected change to be very reliable.

having said that, it is important to realize that all variables created at the command window are public in scope – and this is one cause of the oft-heard cry that “it works in the command window, but not in the exe”. however this is the exception. in all other circumstances, a public variable must be explicitly declared before it is assigned a value and failing to do so will generate an error. this is why you will usually see a public preceded with a release command (see code snippet below). executing the public command physically creates the specified variable(s) and initializes them to .f.

*** declare a couple of public variables

release gnpubnum, gcpubstr

public gnpubnum, gcpubstr

*** until actually assigned a value public variables have the value .f.

gnpubnum = 100

gcpubstr = “test string”

private variables

a private is available to the procedure that created it and any procedures called by the creator, or called in a chain of procedures initiated by the creator. when the creating procedure ends, the variable is automatically released. if no other scope has been specified when a variable is assigned a value, visual foxpro implements it as a private variable. this means that all that is necessary to create a private variable is to assign a value to it:

*** create the following variables as private

pnprinum = 100

pcpristr = “private string”

however, there is a wrinkle to beware of here. if the variable name is already in scope (i.e. it has already been declared as public, or as private in a parent procedure) the assignment statements above do not create new variables, they simply change the value of the existing variable of the same name. this is not always what is wanted and can be very hard to trace when it causes problems. consider the following code:

*** run the child procedure 3 times

for lncnt = 1 to 3

  callchild( lncnt * 4 )

next

 

*** here is the child procedure definition

callchild( tncounter )

for lncnt = 1 to tncounter

  ? “loop number: “ + transform( lncnt )

next

now ask yourself, how many times will the child procedure execute? the answer is, of course, once! the same loop counter name (lncnt) is being used in both the parent and child procedures, with the result that after the first call to the child procedure the value will be 4 (i.e. lncnt = 1 * 4 ) and so the parent loop immediately exits. this is a pretty obvious example of how things can go wrong with private variables – imagine if, instead of a couple of lines below the first, the second use of lncnt was in a different program file executed three or four levels down in the calling chain! debugging what was going wrong would be a nightmare!

fortunately the use of the private command gets around these issues for us. however, unlike the public command it does not immediately create a new variable. instead it tells visual foxpro that when (and if) a value is assigned to the specified variable name, that it must ignore any existing variables and create a new one for which the currently executing procedure is the owner. if you re-use an existing variable name (as i did in the example above) but declare it private before assigning a value and then look in the debugger you will see that vfp still has the original variable but it is now “hidden” and a new one has been created.

note: the parameters statement is equivalent to private. variables defined using this command are created as if they had been declared explicitly as private and any variables with the same names that are in scope are hidden.

local variables

local variables, like private variables are scoped to the procedure that creates them and are released as soon as that procedure terminates. however, unlike private variables, they have no visibility outside of their parent procedure and must be explicitly declared using the local command. when the local command is executed a new variable is immediately created, and initialized to .f. (contrast this with the private command which does not actually create a variable).

the only way in which a local variable can be modified outside of its owning procedure is if it is specifically passed by reference. thus:

*** create a local variable

local lnvar

? lnvar && .f.

lnvar = 100

? lnvar && 100

*** call function and pass by value

func1( lnvar )

? lnvar && still  100

*** call function and pass by reference

func1( @lnvar )

? lnvar  && now it is changed!!!

 

function func1( tnvalue )

tnvalue = 250

return

weak typing and naming

visual foxpro variables are “weakly typed”. this means that even though a variable may be assigned a specific value, it is not restricted to holding only values of that specific data type. in short, variables in visual foxpro are chameleon-like in their ability to adopt the color of their background. the following code is perfectly legitimate in visual foxpro although it most certainly would not be allowed in any strongly-typed language (the result of running this series of commands will be a 6 element array named “lunknown” with today’s date stored in each element):

lunknown = “andy and marcia”

lunknown = 10

dimension lunknown[ 3, 2]

lunknown = date()

weak typing is somewhat of a mixed blessing. since visual foxpro is a data-oriented language, it makes sense to allow variables (especially arrays) to be weakly typed so that data can be transferred easily between tables and memory. however, it does require a more disciplined approach on the part of developers to avoid confusion when working on code. it is strongly recommended that you get into the habit of explicitly declaring the variables you use and that you also adopt a naming convention. the most commonly used convention is one in which the first two characters of a variable’s name indicate it’s scope and intended data type, as shown below:

 

scope identifier

used for

type identifier

used for

g

public (= global)

c

character

p

private

n

numeric (may be either decimal or integer)

 

 

(i, f, d, b)*

special numeric (integer, float, double, binary)

l

local

d

date

t

parameter (private or local)

t

datetime

 

 

l

logical (boolean)

 

 

y

currency

 

 

(m)**

memo

 

 

o

object reference

 

 

a

array (mixed type)

 

 

u

unspecified (unknown or variable)

      * vfp does not, internally, distinguish between these types – all are implemented as “numbers”. however, with data sources that do (e.g. sql server) it is useful to identify the original type

      ** although not strictly a data type, it is often useful to indicate that specific variable is intended to hold the contents of a memo field

 

3 Responses to Scope in Visual Foxpro – Part 1

Leave a Reply

Your email address will not be published. Required fields are marked *