Thursday, September 19, 2013

Using Macros - #localmacro #if and #define

Most of us are familiar with Macros. Unfortunately, they are seldom used to their potential.
From MSDN, 
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.

 static void SimpleDefineIfJob(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.";  

Again from MSDN,
The #localmacro directive is a good choice when you want a macro to have a value that is several lines long, or when your macro value contains a closing parenthesis. The #localmacro directive is a good choice when you want your macro value to be lines of X++ or SQL code.
One particular use i found is when you want to suppress or replace particular where clauses from a select statement. #localmacro works wonderfully.

 static void LocalMacroInSelect(Args _args)  
   VendTable            vendTable;  
   boolean             showAllRecords = false;  
   while select firstOnly10 vendTable  
   if (showAllRecords)  
     #WhileLoopFilter(where vendTable.blocked == CustVendorBlocked::All)  
     #WhileLoopFilter(where vendTable.blocked == CustVendorBlocked::Payment)  

No comments:

Post a Comment