#DEFINE ... #UNDEF Preprocessor Directive

Creates and releases compile-time constants.

You can use the #DEFINE and #UNDEF preprocessor directives to create compile-time constants in programs. By creating constants with #DEFINE instead of using variables, you can reduce memory consumption, increase performance, and simplify programs.

To create a constant with #DEFINE, specify the constant's name with ConstantName and its value with eExpression. When the program is compiled, text substitution is performed and the constant value expression is substituted for the constant name wherever it appears in the program. You can stop the substitution for the constant by issuing #UNDEF.

#DEFINE ConstantName eExpression...
#UNDEF ConstantName

Parameters

  • ConstantName
    Specifies a compile-time constant name. The constant name must be a legitimate Microsoft Visual FoxPro name that begins with a letter or an underscore and consists of up to 254 letters, digits, or underscores. To improve program readability and simplify debugging, capitalize your constant names and use a standard naming convention for them.

    Warning

    Do not use Visual FoxPro keywords for constant names.

    To stop text substitution for a constant created with #DEFINE, issue #UNDEF ConstantName.

  • eExpression
    Specifies the value of the compile-time constant. eExpression can be a name or an expression that evaluates to a character, numeric, currency, date, datetime, or logical value.

    Warning

    Do not use system variables for eExpression. System variables are not evaluated until run time.

Remarks

Visual FoxPro performs substitution only in code that follows the #DEFINE statement that creates the constant and that precedes the #UNDEF statement for that constant. The constant is available only to the program that creates the constant.

If you place #DEFINE within an event or method procedure in a form, the #DEFINE compile-time constant is available within the event or method procedure. Additionally, it may also be available in other events or methods since these code snippets are all written out together to the same Memo field within the form or class library file. You should not depend on this behavior since the order in which methods are written out may change in a subsequent save. To make #DEFINE compile-time constants available to all event and method procedures in a form or class, choose the Include File menu item from the Form (or Class) menu and specify a header file containing the #DEFINE compile-time constants.

Note

Compile time constants are not recognized when placed within quotation marks.

You can redefine a #DEFINE only if you do not change the value. If you change the #DEFINE to a different value, Visual FoxPro generates an error.

Example

The following program creates a compile-time constant named MAXITEMS. This constant is used in a FOR ... NEXT loop to display the numbers 1 through 10.

#DEFINE MAXITEMS 10
CLEAR
FOR gnCount = 1 TO MAXITEMS
   ? gnCount
NEXT

See Also

Reference

COMPILE Command

#IF ... #ENDIF Preprocessor Directive

#IFDEF | #IFNDEF ... #ENDIF Preprocessor Directive

Other Resources

Preprocessor Directives (Visual FoxPro)