FoxPro and Include Files
Constants
If you know that in the normal course of events some things (values) will not change during program execution, many advantages can be found by assigning these values to variables and storing them in an ‘include’ file, then including the file (in various ways) at strategic points within the program.
One advantage is that with careful naming these variables will enhance the readability of your codes.
By way of example
Consider the following two short snippets of code:
IF MESSAGEBOX("Save Record",32+4,"Confirm")=8
{do something that saves the record}
ENDIF
IF MESSAGEBOX("Save Record",gc_Question+gc_YesNo,"Confirm")=gc_Yes
{do something that saves the record}
ENDIF
I think you would agree that the 2nd section of code is easier to understand. However this could be somewhat tedious if you had to declare the variables every time you wished to use them in this manner.
FoxPro (and other languages) allows you to store declared variables in an external file (called an ‘include’ file) and then insert its content at various locations in the program.
Creating an Include File
Include files in general are simply plain text files which contain the definition of predefined variable names and their associated values.
In Foxpro these files usually have a .h or .prg extension. I myself tend to use the .prg, simply so that I do not have to remember to use .h, since VFP by default uses .prg for its extension to command definitions.
Constants.prg
To create an include file called constants using VFP do the following:
From the command window issue
Modify command constants
Then enter the following lines of text and save the file.
*: CONSTANTS *; Value Dialog box buttons #DEFINE gc_OKOnly 0 && 0 OK button only. #DEFINE gc_OKCancel 1 && 1 OK and Cancel buttons. #DEFINE gc_AbortRetryIgnore 2 && 2 Abort, Retry, and Ignore buttons. #DEFINE gc_YesNoCancel 3 && 3 Yes, No, and Cancel buttons. #DEFINE gc_YesNo 4 && 4 Yes and No buttons. #DEFINE gc_RetryCancel 5 && 5 Retry and Cancel buttons. *;Value Icon #DEFINE gc_stop 16 && 16 Stop sign. #DEFINE gc_Question 32 && 32 Question mark. #DEFINE gc_Exclamation 48 && 48 Exclamation point. #DEFINE gc_Information 64 && 64 Information (i) icon. *; Value Default button #DEFINE gc_1st_button 0 && 0 First button. #DEFINE gc_2nd_button 256 && 256 Second button. #DEFINE gc_3rd_button 512 && 512 Third button. *; Return Values #DEFINE gc_OK 1 #DEFINE gc_Cancel 2 #DEFINE gc_Abort 3 #DEFINE gc_Retry 4 #DEFINE gc_Ignore 5 #DEFINE gc_Yes 6 #DEFINE gc_No 7
Including an Include File
Once you have created your include file(s) you can then insert them at strategic places within you source code.
Much depends on exactly what you are trying to do, and the scope (how long they remain in existence) of the defined variable in question.
In its simplest form a file can be included in another .preg file by an include declaration
#INCLUDE constants.prg
This will tell foxpro to ‘include’ the declaration at this point in the program, and then they will be available (within that program) to use until the program ends at which time they will cease to exist.