Constants in C# and VFP

C#

Const, Readonly

VFP

#DEFINE, #INCLUDE, #UNDEF

C# Syntax Notes

Constants in C# have their value fixed at compile time:

const double VAT_Rate = 0.175;

Fields can also be defined as being readonly to give a little more flexibility:

double readonly VAT_Rate;

The value of a readonly field can be set in the constructor of its class but is then fixed for the lifetime of the class. In this example, the VAT rate might be being read from some external data source as the class is being created.

VFP Syntax Notes

Constants in Visual FoxPro exist from the moment that they are defined. After this declaration:

#DEFINE CTRL_C 3

the compiler will replace all occurrences of the characters 'CTRL_C' with the value 3.

Do not try to put an '=' sign into this declaration. It's a great temptation, but it's wrong.

Be aware that there is no sanity check on this process, it's just a mindless search and replace through the source code. You can choose to redefine a FoxPro command:

#DEFINE BROWSE "DELETE"

This way lies madness.

FoxPro has an #UNDEF directive which stops the replacement process. This can be placed anywhere in any of the files of a project and means that constants can be made local to any region of code - down to perhaps a few lines in one procedure. This was useful in the days of long monolithic programs but is rarely used now.

FoxPro also has the #INCLUDE directive which pulls a header file of constants into the program. The FoxPro.H file supplied with the language includes the definitions of many useful constants such as COLOR_BLUE. It's useful to create a standard include file for your own project and to include FoxPro.H into this file.

Foxpro include files

#INCLUDE FoxPro.H

*-- This indicates that there are no entries here - it starts with a "-" so that
*-- it sorts to the head of any list. Similarly for all entries.

#DEFINE NOENTRIES "-None-"
#DEFINE ALLENTRIES "-All- "


*-- We need something more meaningful than .F. and .T. when displaying
*-- Boolean values.

#DEFINE APP_FALSE "False"
#DEFINE APP_TRUE "True"

Comments  |  Language index  |  Variables

More tips from Alvechurch Data

More tips from Alvechurch Data

Access VBA constants

Using constants in Access VBA.

Read More

Access CommandBarButtons

Access VBA constants for CommandBarButtons in a toolbar.

Read More

VBA and VFP constants

Visual Basic and FoxPro program constants.

Read More