Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Use of macro substitution vs. string valued variables
Message
From
22/07/1999 00:43:10
 
 
To
21/07/1999 19:14:23
David Fluker
NGIT - Centers For Disease Control
Decatur, Georgia, United States
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00244254
Message ID:
00244557
Views:
22
Hi David,

Thank you very much for the wonderful explanation, which made a lot of fuzzy things clear after all these years. However, after playing in the command window for a while, something new begin to puzzle me…

I am not surprised by the following:
coldsafety = set("safety")
set safety &coldsafety && Working
set safety (coldsafety) && NOT Working
set safety eval("coldsafety") && NOT Working


However, I am surprised by:
colddate = set("date")
set date &colddate && Working as usuall
set date (colddate) && WOKING!!!
set date eval("colddate") && WOKING!!!


What are going on here?


Chen



> --------------------------------------
>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.
Chen Nan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform