Show/Hide Toolbars

XSharp

Purpose

Declare a constant name and its value to the compiler.

Syntax

[Modifiers] DEFINE <idConstant> := <uValue> [AS <idType>]

Arguments

ModifiersAn optional list of modifiers that specify the visibility or scope of the entity, such as PUBLIC, STATIC, INTERNAL, EXPORT and UNSAFE.
<idConstant>A valid identifier name for the constant.  A constant is an entity and, as such, shares the same name space as other entities.  This means that it is not possible to have a constant and a global variable, for example, with the same name.

 

<uValue>A constant value that is assigned to <idConstant>.  This value can be a literal representation of one of the data types listed below or a simple expression involving only operators, literals, and other DEFINE constants; however, more complicated expressions (including class instantiation) are not allowed.

 

AS <idType>Specifies the data type.  If omitted, then depending on the compiler options the type will be either USUAL or determined by the compiler.

 

 

Description

Once the constant name and value is declared and initialized with the DEFINE statement, you can not change the value of <idConstant> without provoking a compiler error.  The constant value <uValue> will be used whenever the <idConstant> identifier name is encountered in your application.

 

You can hide a constant name from a routine by declaring a variable with the same name (with LOCAL, MEMVAR, or FIELD).  The search order for a variable name is as follows:

1.        LOCALs, local parameters, MEMVARs, and FIELDs

2.        SELF instance variables (i.e., without <idObject>:  prefix in class methods)

3.        GLOBALs and DEFINEs

 

Tip: You can perform a conditional build based on the value of a DEFINE constant.  See the #ifdef and #ifndef statements in this chapter for more information and examples.

Examples

The following example assigns an application name to the constant cAppName.  This value is then displayed at the beginning and end of the application run:

 

DEFINE cAppName := "Accounts Payable"
...
FUNCTION Start()
  ? "Start of ", cAppName, " application."
  ...
  ? "End of ", cAppName, " application."

See Also

#ifdef, #ifndef, GLOBAL