How to Scope a Variable to a Form

Last reviewed: April 30, 1996
Article ID: Q126952
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

SUMMARY

This article explains how to declare variables that exist and are visible throughout a form but are released when the form is released.

MORE INFORMATION

A variable declared in a method of a form is visible while the method is executing. Any reference to this variable after the method exits will yield a 'Variable not Found' error message. In Visual FoxPro, the Form Designer builds a Form class, and a Form is an object. Variables declared in a method such as the Init or Load are scoped to the method. A variable scoped to a Form object has to be a member variable, or property. This differs from the version 2.x model, where variables declared in the Setup of a screen would be generated at the beginning of the .SPR file, and be available to the program and procedures that the program calls.

Use the following steps to add a new property to a Form in the Form Designer:

  1. Choose New Property from the Form menu.

  2. Type the name of the member variable, and click OK in the dialog. The property is now available at the bottom of the property sheet.

  3. To refer to the property from an object in the Form, you can:

    - Use the This object reference at the Form level. For example:

            This.chvar1
    
       - Use the ThisForm object reference at the Object level. For example:
    
            ThisForm.chvar2
    
       - Use the name of the Form reference. For example:
    
            Frmtest.chvar1
    


Additional reference words: VFoxWin 3.00 screen private
KBCategory: kbtool
KBSubcategory: FxtoolFormdes


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: April 30, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.