Local Functions [AX 2012]
Updated: July 20, 2012
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
You can declare local functions inside a method or a job in X++. A local function can contain code that would otherwise have to be duplicated in two or more places within your method.
Tip |
|---|
|
The best practice is to add private methods to the class rather than to add local functions inside the method. |
The following X++ job code example shows valid declarations of two local functions named localFunc55b and localFunc66c. Calls to the local functions occur after the function declarations in the code example, as is required.
static void G_LocalFuncJob2(Args _args) // X++ job.
{
int nn = 654;
void localFunc55b(int _iNum) // The local function.
{
str sInnerString;
sInnerString = "String_in_localFunc55b";
info(strFmt("localFunc55b: %1 , %2 , %3",
_iNum, sInnerString, nn));
}
void localFunc66c()
{
info("Printing from inside localFunc66c.");
}
; // This semicolon marks the end of local function declarations.
localFunc55b(55);
localFunc66c();
// Next print statement would fail to compile,
// because sInnerString is restricted to the
// scope of the local function in which it is declared.
//print sInnerString; pause;
}
/*** Infolog window display:
Message (07:38:54 pm)
localFunc55b: 55 , String_in_localFunc55b , 654
Printing from inside localFunc66c.
***/
-
A local function can be called only by code in the same method or job where the local function is declared.
-
An X++ local function should never call itself. Such recursion can prevent the successful compile of X++ to .NET Framework CIL.
Note X++ local functions are transformed into inline code when the X++ method is compiled into CIL.
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.