Show/Hide Toolbars

XSharp

Purpose

Syntax

#define identifier [token-string]

or

#define identifier LPAREN parameters RPAREN

 

#define lets you define a symbol, such that, by using the symbol as the expression in a #ifdef directive, the expression will evaluate to true or in a or #ifndef directive the expression will evaluate to false.

#define also allows you define a symbolic name for a token string, so you can use the symbolic name in your code and the preprocessor will replace all occurences of that name with the token string that you have specified.

For example:

#define DEBUG
// ...
#if DEBUG
   Console.WriteLine("Debug version");
#endif

 

or

 

#define VERSION "1.2.0.0"
#define FILEVERSION "1.2.3.4"
// ...
[assembly: AssemblyVersion(VERSION)]
[assembly: AssemblyFileVersion(FILEVERSION)]

A define with parentheses, such as

 

#define MAX(a,b) iif(a>b, a, b)

 

will be treated like a #translate.

Notes

Please note that defines are CASE SENSITIVE, so the following code will work:

 

#define TEST 123
 
FUNCTION Test() AS INT
  RETURN TEST

but this will NOT compile:

#define TEST 123
 
FUNCTION TEST() AS INT
  RETURN TEST

because the preprocessor will replace the name TEST in the FUNCTION line with the value 123 which is not a valid identifier. After preprocessing the code becomes:

FUNCTION 123() AS INT
  RETURN 123