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.


   1:  static void ClosingBracketInMacroDefinition(Args _args)
   2:  {
   3:      //#define.Question("Why are brackets ')' not working ?")
   4:      //#define.Question(@"Why are brackets ')' not working ?")
   5:      //#define.Question("Why are brackets '\)' not working ?")
   6:      #define.LegalCharacters(' !"#$%&\'(*+,-./:;<=>?@[\\]^_`{|}~\n\r\t')
   7:      #localmacro.Question
   8:          "Why are brackets ')' not working ?"
   9:      #endmacro
  10:      
  11:      Box::info(#Question);
  12:      Box::info(#LegalCharacters);
  13:  }

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