A macro is a variable known to the precompiler. The variable can have a value that is a sequence of characters, but it is not required to have a value. The #define directive tells the precompiler to create the macro variable, including an optional value. The #if directive tests whether the variable is defined, and optionally, whether it has a specific value.
The value of a macro variable can be written into the X++ code at any location, by giving the name of the macro with the # character added as a prefix.
All precompiler directives and symbols begin with the # character. All of the directives and symbols are handled and removed before the resulting X++ code is given to the X++ compiler.
Simple Macro example of defining
static voidSimpleDefineIfJob(Args _args)
{
str sTest = “Initial value.”;
;
#define.MyMacro // MyMacro is now defined.
#if.MyMacro
sTest = “Yes, MyMacro is defined.”;
info(sTest);
#endif
// Notice the non-code sentence line causes no X++ compiler error,
// because the X++ compiler never sees it.
#ifnot.MyMacro
The X++ compiler would reject this sentence.
sTest = “No, MyMacro is not defined.”;
info(sTest);
#endif
/********** Actual output
Message (03:46:20 pm)
Yes, MyMacro is defined.
**********/
}