How to: Test a Macro Value [AX 2012]

Updated: October 1, 2009

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

You can test a macro to see whether it has a value. You can also test to see whether its value is equal to a specific sequence of characters. These tests enable you to conditionally include lines of code in your X++ program.

For this topic, you must understand the information in How to: Use a Macro Value.

There is no way you can test whether a defined macro has a value. You can only test whether a specific value matches the value of a macro.

TipTip

Any macro name that you define should always have a value, or it should never have a value. When you alternate between these modes, your code becomes difficult to understand. For macros that have a value, you can vary the value when you see fit.

In the following code sample, two #if tests are run to determine whether the macro MyIntMacro exists. The #if.MyIntMacro() test is true. This syntax behaves the same as #if.MyIntMacro.

static void TestMacroValue6Job(Args _args)
{
    ;
    #define.MyIntMacro(66)

    // #if tests.

    #if.MyIntMacro()
        info("A: " + int2str(#MyIntMacro));
    #endif

    #if.MyIntMacro(66)
        info("B: " + int2str(#MyIntMacro));
    #endif

    // #ifNOT tests.

    #ifNOT.MyIntMacro(7777)
        info("C: " + int2str(#MyIntMacro));
    #endif

    #ifNOT.No_Such_Macro_Name(66)
        info("D: " + int2str(#MyIntMacro));
    #endif

/****************  Actual output
Message (11:24:47 am)
A: 66
B: 66
C: 66
D: 66
****************/
}

The following code sample shows the #if.DebugMacro(heavy) directive that tests the value of the DebugMacro macro. If the value is the five character sequence heavy, then the test is true.

static void TestMacroSpecificValue8Job(Args _args)
{
    ;
    // Uncomment either one of these defines, or neither.
    //#define.DebugMacro(light) // This line for: light debugging.
    #define.DebugMacro(heavy) // This line for: heavy debugging.

    #if.DebugMacro
        info("Starting the job.");
    #endif

    #if.DebugMacro(heavy)
        info("UTC == "
            + DateTimeUtil ::toStr
                (DateTimeUtil::utcNow()
                )
            );
    #endif

    // Do something useful here.

/**********  Actual output
Message (01:58:12 pm)
Starting the job.
UTC == 2007-12-05T21:58:12
**********/
}

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.

Community Additions

ADD
Show: