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.

TipTip

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.
***/

  • The local functions must be declared physically above any non-declaration statements that exist in the method or job.

  • You can declare more than one local function in your method. But all local functions must be declared in an uninterrupted series, with the set terminated by one semicolon.

  • Code that is inside the local function can access variables that are declared in the method or job that contains the local function.

  • Code that is outside the local function cannot access variables that are declared in the local function.

  • 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.

    NoteNote

    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.

Community Additions

ADD
Show: