PRB: DO WHILE Loop Does Not See Changes Made to Macro Variable

Last reviewed: February 21, 1996
Article ID: Q139156
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b

SYMPTOMS

When you assign a field name to a variable and use a DO WHILE command on the variable with macro substitution, the DO WHILE command doesn't recognize any changes made to the variable while in the DO WHILE loop. For example, if the variable x has been assigned a field name, then using DO WHILE &x will evaluate the contents of the field. But if the contents of the variable changes to another field name inside the DO WHILE loop, the new field name in the variable is not recognized when the DO WHILE command is looped through again. This could cause the DO WHILE loop to cycle indefinitely.

CAUSE

The behavior of DO WHILE loops with macro substitution is controlled by the SET COMPATIBLE setting. With the syntax SET COMPATIBLE OFF, Visual FoxPro only evaluates the macro substitution the first time it goes through the loop. This is significantly faster than evaluating it each time through the loop. With SET COMPATIBLE ON, Visual FoxPro evaluates the macro substitution each time through the loop.

RESOLUTION

Use the following code instead. This code will stop when the variable x is assigned a different field because the DO WHILE command is evaluating the actual variable y instead of a macro substituted variable.

   CREATE TABLE test ;
          (fld1 C(10), fld2 C(5))
   INSERT INTO test ;
          (fld1, fld2) VALUES ("abcdef", "abcde")
   x = "fld1"
   y = &x
   DO WHILE y = "abcdef"
      WAIT WINDOW y
      x = "FLD2"
      y = &x
   ENDDO

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

Run the following program:

   CREATE TABLE test ;
       (fld1 C(10), fld2 C(5))

   INSERT INTO test ;
       (fld1, fld2) VALUES ("abcdef", "abcde")

   x = "fld1"
   DO WHILE &x = "abcdef"
      WAIT WINDOW &x
      * Note that the Wait Window shows the correct contents each time.
      x = "fld2"
   ENDDO


Additional reference words: 3.00 3.00b VFoxWin
KBCategory: kbprg kbprb kbcode
KBSubcategory: FxprgMacrosub


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 21, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.