Visual Basic and Visual FoxPro variables


Dim, Private, Public


Local, Private, Public

Visual Basic Syntax Notes

Variables in Visual Basic have types. You can declare a variable without specifying an explicit type:

dim strName

but all that will happen is that the variable will be declared to be of type variant by default. This is bad practice because operations involving variants are always slower than the same operation on a variable of the correct type.

Use the Dim, Private or Public to declare a variable. This is not compulsory unless you have specified Option Explicit in the General section of the code module but declaring a variable protects you against silly typing errors so Option Explicit is a wise precaution.


Variables declared with Dim will be restricted to the procedure, function or method in which they are declared. Variables declared as Public will be available throughout the application. A Private variable will available throughout the module in which it is declared.

Visual FoxPro Syntax Notes

All Visual FoxPro variables are of the same general type and you can change the type of data stored in a variable after it has been declared. The same variable might store text, numbers, dates or booleans at different stages of the program but this is poor programming practice and leads to confusion.

If you assign a value to a FoxPro variable without having declared it then that variable will immediately be created and be given Private scope. This means that a typing error in a FoxPro program can easily lead to the creation of a new variable.


FoxPro is not strongly typed but Visual FoxPro allows you to state the type of a variable when you declare it but the types are not checked at design or runtime. This code compiles and runs:

Local intX as Integer
Local strY as String
intX = "abc"
strY = 12
?intX, strY


Variables declared with Local will be restricted to the program, procedure, function or method in which they are declared. Variables declared as Public will be available throughout the application and will remain in existence after the program has finished execution. This can be a source of obscure bugs during development because public variables from one run of the program will exist and have a value at the start of the next run. This of course can only happen in the development environment and so the application can behave differently at design and at runtime.

The Private scope predates Local and it and Public were the only scopes available before the launch of Visual FoxPro. It restricts the variable to the program, procedure, function or method in which it is declared and to anything which is called from there. This mechanism is sometimes used as a lazy alternative to parameter passing.

Private has two valid uses. One is to allow a variable to be printed in a report, the other is as a safer alternative to Public. If a variable is declared as Private in the top-level program of an application then it will behave very much like a Public variable and it will be available to all modules of that application. It will however cease to exist at the end of the application and will be created anew when the application runs again.

Constants  |  Language index  |  Arrays