Macros in Dynamics AX X++

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.
        sTest = “Yes, MyMacro is defined.”;
    // Notice the non-code sentence line causes no X++ compiler error,
    // because the X++ compiler never sees it.
        The X++ compiler would reject this sentence.
        sTest = “No, MyMacro is not defined.”;
/********** Actual output
Message (03:46:20 pm)
Yes, MyMacro is defined.

Leave a Reply

Your email address will not be published. Required fields are marked *