Export (0) Print
Expand All

X++, C# Comparison: Loops [AX 2012]

Updated: March 30, 2011

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

This topic compares the loop features between X++ and C#.

The following features are the same in X++ and C#:

  • Declarations for variables of the int primitive data type. Declarations for other primitive types are almost the same, but the types might have different names.

  • while statement for loops.

  • break statement to exit a loop.

  • continue statement to jump up to the top of a loop.

  • <= (less than or equal) comparison operator.

The following table lists X++ features that are different in C#.

Features

X++

C#

Discussion

The for statement.

The for statement is available for loops.

The C# for statement is slightly different from for in X++.

In C# you can declare the counter integer in the for statement. But in X++ the counter must declared outside the for statement.

++ increment operator.

An ++ increment operator is available in X++. But an int variable that is decorated with ++ can only be used as a statement, not as an expression. For example, the following lines of X++ code would not compile:

int age=42; print age++;

However, the following lines of X++ code would compile:

int age=42; age++; print age;

The C# ++ operator is more flexible than in X++.

The following lines of code are the same in both languages:

  • ++ myInteger;

  • myInteger++;

But the following lines of code have a different effect from each other, and are valid only in C#:

  • yourInt = ++myInt;

  • yourInt = myInt++;

modulo operator.

In X++ the modulo operator is mod.

In C# the modulo operator is %.

The symbols for the modulo operator are different, but their behavior is the same in both languages.

Temporarily suspend a console program that has already begun.

The pause statement.

In C#, a command line program can be paused by the following line of code:

  • Console.In.Read();

In X++ you continue by clicking an OK button on a modal dialog box. In C# you continue by pressing any keyboard on the keyboard.

Display a message.

In X++, the print statement displays a message in the Print window.

In C# a message can be displayed on the console by the following line of code:

  • Console.WriteLine();

The X++ print function is used only when you test. An X++ program that uses print almost always uses the pause statement somewhere later in the code.

For production X++ code, use the Global::info Method instead of print. The strfmt function is often used together with info. There is no reason to use pause after info.

Make a sound.

The beep function makes a sound that you can hear.

In C# a sound that you can hear is issued by the following line of code:

  • Console.Beep();

The statements each produce a short tone.

The X++ code samples in this topic use the print function to display results. In X++ you can use the print statement can display any primitive data type without having to call functions that convert it to a string first. This makes print useful in quick test situations. Generally the Global::info method is used more often than print. The info method can only display strings. Therefore the strfmt function is often used together with info.

A limitation of print is that you cannot copy the contents of the Print window to the clipboard (such as with Ctrl+C). Global::info writes to the Infolog window which does support copy to the clipboard.

The while keyword supports looping in both X++ and C#.

static void JobRs002a_LoopsWhile(Args _args)
{
    int nLoops = 1;
    while (nLoops <= 88)
    {
        print nLoops;
        pause;
        // The X++ modulo operator is mod.
        if ((nLoops mod 4) == 0)
        {
            break;
        }
        ++ nLoops;
    }
    beep(); // Function.
    pause; // X++ keyword.
}

The output in the X++ Print window is as follows:

1
2
3
4

using System;
public class Pgm_CSharp
{
    static void Main( string[] args )
    {
        new Pgm_CSharp().Rs002a_CSharp_ControlOFlowWhile();
    }
    void Rs002a_CSharp_ControlOFlowWhile()
    {
        int nLoops = 1;
        while (nLoops <= 88)
        {
            Console.Out.WriteLine( nLoops.ToString() );
            Console.Out.WriteLine( "(Press any key to resume.)" );
            // Paused until user presses a key.
            Console.In.Read();

            if ((nLoops % 4) == 0) break;
            ++ nLoops;
        }
        Console.Beep();
        Console.In.Read();
    }
}

The console output from the C# program is as follows:

[C:\MyDirectory\]
>> Rosetta_CSharp_1.exe
1
(Press any key to resume.)

2
(Press any key to resume.)
3
(Press any key to resume.)

4
(Press any key to resume.)

The for keyword supports looping in both X++ and C#.

In X++ the counter variable cannot be declared as part of the for statement.

static void JobRs002a_LoopsWhileFor(Args _args)
{
    int ii; // The counter.
    for (ii=1; ii < 5; ii++)
    {
        print ii;
        pause;
        // You must click the OK button to proceed
        // beyond a pause statement.

        // ii is always less than 99.
        if (ii < 99)
        {
            continue;
        }
        print "This message never appears.";
    }
    pause; // X++ keyword.
}

The output in the X++ Print window is as follows:

1
2
3
4

using System;
public class Pgm_CSharp
{
    static void Main( string[] args )
    {
        new Pgm_CSharp().Rs002a_CSharp_ControlOFlowFor();
    }

    void Rs002a_CSharp_ControlOFlowFor()
    {
        int nLoops = 1,
            ii;

        for (ii = 1; ii < 5; ii++)
        {
            Console.Out.WriteLine(ii.ToString());
            Console.Out.WriteLine("(Press any key to resume.)");
            Console.In.Read();

            if (ii < 99)
            {
                continue;
            }
            Console.Out.WriteLine("This message never appears.");
        }
        Console.Out.WriteLine("(Press any key to resume.)");
        Console.In.Read();
    }
}

The console output from the C# program is as follows:

1
(Press any key to resume.)

2
(Press any key to resume.)
3
(Press any key to resume.)

4
(Press any key to resume.)
(Press any key to resume.)

Announcements: To see known issues and recent fixes, use Issue search in Microsoft Dynamics Lifecycle Services (LCS).

Community Additions

ADD
Show:
© 2014 Microsoft