Microsoft KB Archive/131475

= Use #INCLUDE to Add Header Files to a Visual FoxPro Proj =

Article ID: 131475

Article Last Modified on 2/15/2000

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q131475



SUMMARY
By using the #INCLUDE directive, you can instruct Visual FoxPro to include the contents of another file such as a header file (.H file) in your code during compilation. Header files have useful items such as standard constants that you can use in your Visual FoxPro code. This ability has existed in the C programming language. Now it is also available to Visual FoxPro developers.



Benefits of Using #INCLUDE
Coding problems can be minimized when the #INCLUDE generator directive is used to include header files. Constants can be given values once in the header file, and these constants can be used anywhere in FoxPro code.

By definition, the value of PI is 3.14. If a named constants header file is created with a #DEFINE for PI, the word PI could be used rather than typing 3.14. Using the named constant, Visual FoxPro replaces all occurrences of PI with 3.14 at compile time.

Using English words like PI in code improves readability and can make localization for non-English countries easier. For example, a single constants file could be created that contains application messages as follows: #DEFINE CANCELENTRY "Are you sure you want to cancel this record?" Then, use CANCELENTRY in your Visual FoxPro code. To translate your program for a different country, all you need to do is translate a copy of the constants file into the different language. No changes to your Visual FoxPro code are necessary.

Example One: COMMON.H
A Named Constants file (FOXPRO.H) comes with Visual FoxPro. It contains the named constants for most Visual FoxPro functions. You can extend this concept of named constants even further by creating a COMMON.H file to define a number of standard types of constants such as the character values required for a carriage return/linefeed.

The following sample header file demonstrates this concept: * COMMON.H


 * -- ASCII

If you use #INCLUDE to include this COMMON.H file in your Visual FoxPro project, you can use the text CRLF at any point in your program code where a carriage return/linefeed combination is needed. You no longer need to type CHR(13)+CHR(10). While either choice (CRLF or CHR(13)+CHR(10)) is valid, if a defined constant should change values, it need only be changed in a single location for that change to be reflected at all locations in your code where the constant is referenced.
 * 1) DEFINE LF             CHR(10)
 * 2) DEFINE CR             CHR(13)
 * 3) DEFINE CRLF           CR+LF

Example Two: TESTFILE.PRG
Building on the COMMON.H file, the following code provides a simple demonstration of the use of a COMMON.H file and constants referenced in code: *---*
 * Sample program : TESTFILE.PRG                            *
 * 1) INCLUDE "COMMON.H" && Use definitions from COMMON.H file
 * 1) INCLUDE "COMMON.H" && Use definitions from COMMON.H file

ACTIVATE SCREEN CLEAR ? 'Line 1'+CRLF+'Line 2' =TestUDF

FUNCTION TestUDF ? DRAG_ENTER           && refer to FOXPRO.H file RETURN


 * Sample Header File: COMMON.H                             *
 * -- ASCII
 * 1) DEFINE LF             CHR(10)
 * 2) DEFINE CR             CHR(13)
 * 3) DEFINE CRLF           CR+LF
 * 1) DEFINE CR             CHR(13)
 * 2) DEFINE CRLF           CR+LF

** must qualify this file with the ** appropriate path location
 * -- Header files
 * 1) INCLUDE "FOXPRO.H" ** If not in current directory, you

Example Three: Using FOXPRO.H that Comes with Visual FoxPro
Here's another example use of named constants in code. As found in the FOXPRO.H file, the following constants for the MessageBox function can be used in place of the numeric equivalents. This list has been abbreviated for space considerations: *-- Function Parameters
 * -- MessageBox parameters
 * 1) DEFINE MB_YESNO         4    && Yes and No buttons
 * 2) DEFINE MB_ICONQUESTION  32   && Warning query
 * 3) DEFINE MB_DEFBUTTON2    256  && Second button is default

Given the above definitions, a sample dialog box written without the named constants might read as follows: cMessageTitle = 'My App' cMessageText = 'Record not found. Would you like to search again?' nDialogType  = 4 + 32 + 256  && The appearance of the message box *   4 = Yes and No buttons *  32 = Question mark icon * 256 = Second button is default nAnswer = MESSAGEBOX(cMessageText, nDialogType, cMessageTitle)
 * -- MsgBox return values
 * 1) DEFINE IDYES            6    && Yes button pressed
 * 2) DEFINE IDNO             7    && No button pressed

IF (nAsnswer = 6)  && Yes was selected && Execute some other code ENDIF The nDialogType code could be recast using named constants as this: cMessageTitle = 'My App' cMessageText = 'Record not found. Would you like to search again?' nDialogType  = MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2

nAnswer = MESSAGEBOX(cMessageText, nDialogType, cMessageTitle)

IF (nAnswer=IDYES) && Execute some other code ENDIF Both ways are equally valid, but by using words as opposed to numbers, the code is easier to understand and maintain.

