Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Use of macro substitution vs. string valued variables
Message
From
21/07/1999 19:14:23
David Fluker
NGIT - Centers For Disease Control
Decatur, Georgia, United States
 
 
To
21/07/1999 13:33:40
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00244254
Message ID:
00244452
Views:
37
>Reading through the recent thread #243690 about macro substitution reminded me of the more general question I have about when to use macro substitution as opposed to using a string valued variable...
>
>At any rate, though I have a feel for how and when to use macro substitution, I'm not sure how I'd state the specific rules for how and when to use it and I think there might be some cases where using either a macro or string valued variable would be legal but would produce different results.
>
>Is there a basic rule of thumb to go by?
--------------------------------------
David,

Once you know what is going on behind the scenes in a macro substition, it will make more sense.
FoxPro pre-compiles code into tokens which are sent to the runtime processor. This helps FoxPro code run quickly, even though it is an interpreter and not an compiled language.

PROCESSING MACROS
When the FoxPro compiler encounters a &, it does not tokenize that line of code. If you look in an fxp you can read the lines of code which contain macros. When the program is running, it stops momentarily at that line of code, replaces the macro symbol and variable with the contents of the macro variable, sends that line to the compiler to be tokenized, then sends it on to the interpreter. Notice that to the interpreter, there is no difference between a macro expansion and code that was typed at design time! This means a macro can contain anything; values, commands, entire lines of code.

PROCESSING NAME EXPRESSIONS
Enclosing a variable in parenthesis forces the interpreter to evaluate the string expression inside. So if cParam = "Customer" then ? (cParam) displays "Customer". Big Deal! So does ? cParam, and I saved two keystrokes. BUT, FoxPro commands often contain something called Names. In the command "USE Orders IN 0" Orders is the name. If you want to USE the Customer table, and cParam = 'Customer', the line USE cParam IN 0 will try to open a table called cParam. You can use cParam as a NAME EXPRESSION by enclosing it in parenthesis, forcing the interpreter to evaluate it. "USE (cParam) IN 0"

WHAT'S THE DIFFERENCE?
If cParam = "Customer", both of these lines open the Customer table
USE &cParam IN 0 and USE (cParam) IN 0
The difference is, to process the macro, FoxPro had to stop the line parser when it recognized an untokenized line, evaluate and replace the macro, tokenize the line, and then send it to the interpreter. It is MUCH SLOWER. The Name Expression, on the otherhand, can be precompiled, and sent straight to the interpreter where the work is done. Name expressions process significantly faster.

WHEN DO I USE A MACRO
Back to the original example "SET EXCLUSIVE $myOffOnStringVariable" The ON and OFF in SET EXCLUSIVE are part of the command. They are not names, and not parameters that can be passed as a variable. That is why just including the variable, or even enclosing it in parenthesis, give a syntax error. SET EXCLUSIVE myOffOnStringVariable is not a valid FoxPro command. By putting a & in front of the variable, the compiler ignores it, so ther is no error generated, and when the interpreter finally gets it, it is a valid, tokenized command. So, a macro can it all, values or commands. IMPORTANT NOTE! To speed up DO WHILE and FOR and SCAN loops, Macros are only evaluated at the top of the loop and will not change, even if the value of the variable changes, during the loop.

THE RULE OF THUMB (Finally)
When ever possible, use Name Expressions, for file names, window names, menu names. Similarly, to return the value of a variable referenced by another variable, use EVALUATE. For example, to print the value of fields in a table - If cParam = "LastName" then EVALUTE(cParam) returns the contents of the lastname field or variable. If cParam = 'ZipCode' then EVALUATE(cParam) returns the value of ZipCode.
When a name expression, or EVALUATE() will not work, such as in a command, use a macro expansion. Macros are a very paowerful part of FoxPro. You can build entire lines of code on the fly, such as an SQL-Select statement, and execute them using Macro Expansion.

David Fluker.
David.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform