Thursday, February 23, 2012

Tip: Illegal 'closing bracket' character when defining a macro

Just a very quick tip today, related to macros:


As you all know, there are multiple ways to define and use macros in X++.
For those that need a refresher, please look up the corresponding section on MSDN
(Direct link: http://msdn.microsoft.com/en-us/library/cc197107.aspx)

Below is a simple X++ job, that demonstrates an existing shortcoming in the #define command, and a possible workaround for this problem.

Nothing complicated, basically, just use #localmacro, if you can't compile your code.

static void ClosingBracketInMacroDefinition(Args _args)
{
    //#define.Question("Why are brackets ')' not working ?")
    //#define.Question(@"Why are brackets ')' not working ?")
    //#define.Question("Why are brackets '\)' not working ?")
    #define.LegalCharacters(' !"#$%&\'(*+,-./:;<=>?@[\\]^_`{|}~\n\r\t')
    #localmacro.Question
        "Why are brackets ')' not working ?"
    #endmacro

    Box::info(#Question);
    Box::info(#LegalCharacters);
}

Thanks for finding the issue to Bogdana, one of our new developers.