Exploring X++ Control Statements for Microsoft Dynamics AX 2012

By - May 23, 2013

Programming control statements are the source of many operations within Microsoft Dynamics AX 2012. This article explores four of the most popular statements that are used to manipulate and retrieve data.  In this case, we’ll explore possibilities using a familiar tool, the Magic Eight Ball. The Magic Eight Balltm  by Mattel is a mysterious device that has many different solutions to all of life’s questions, with a simple shake. The mystery continues hidden beneath the murky blue netherworld where there are many more choices than expected. The following classes use, the if (elseif) statement, the switch (case) statement, the select statement, and finally the while statement combined with the select statement to randomly answer questions from a list of values. For the last two steps use a table to store the data set, which is typical in AX Programming.  For each example, the common methods are used for each of these classes. The print method, which often acts as the get, is illustrated for each case to show the differences in the programming methodology.

Common Methods

The first part is the method that all of these classes have in common.  They cover the basics of the classDeclaration, new, main, and introMessage.  Also included is a snippet showing the table that is used for the While and Select classes.

class crazyLuckyIf OR class crazyLuckySwitch  OR

class crazyLuckySelect OR class crazyLuckyWhile

{

str 30 answer;

int luckyNumber;

int minVal;

int maxVal;

} 

private void new()

{

minVal = 1;

maxVal = 20;

}

public static void main(Args args)

{

crazyLuckyIf eightBall = new crazyLuckyIf();

eightBall.introMessage();

eightBall.luckyNumberGenerator();

eightBall.printMessage();

}

public void introMessage()

{

Box::info("Welcome to the magic eightball, think of your wish and press OK",

"Wish","Think of a yes/no question then press OK");

} 

public void luckyNumberGenerator()

{

RandomGenerate randomNumber = new RandomGenerate(); 

luckyNumber = randomNumber.randomInt(minVal,maxVal);

}

Here is the table used for the crazyLuckyWhile class and the crazyLuckySelect class.

Exploring X++ Control Statements AX2012-pic1

If Statement

One of the oldest and most used programming language items is the if statement.  It appears in classic languages, logic trees, databases, and even simple spreadsheets.  The if statement for X++ supports all of this functionality including the elseif.  The following class method exemplifies this.

public void printMessage()

{

if (luckyNumber == 1)

{

answer = "It is certain";

}

else if (luckyNumber == 2)

{

answer = "It is decidedly so";

}

else if (luckyNumber == 3)

{

answer = "Without a doubt";

}

else if (luckyNumber == 4)

{

answer = "Yes - definitely";

}

else if (luckyNumber == 5)

{

answer = "You may rely on it";

}

………………………..And the rest of the choices.

}

Box::info (strFmt(answer),"Answer","This is the answer to your  

question");

}

Switch Case Statement

When it comes to database design, switch case often shows up as a better choice for a list.  In this case, it uses much of the same logic as the if statement, however, it accomplishes the task in less characters and the code is easier to read.  Also in this example, there is an answer set to a label, which is a much better design because the label can be translated to a multitude of language types supporting global AX implementation.

public void printMessage()

{

switch(luckyNumber)

{

case 1:

answer = "@SDT251";

break;-

case 2:

answer = "It is decidedly so";

break;

case 3:

answer = "Without a doubt";

break;

case 4:

answer = "Yes - definitely";

break;

case 5:

answer = "You may rely on it";

break;

case 6:

answer = "As I see it, yes";

break;

………………………..And the rest of the choices.

}

Box::info (strFmt(answer),"Answer","This is the answer to your  

question");

}

Select Statement

The select statement allows us to be able to find in the table answernumber and answer where it matches luckyNumber.  This is typical of programming in X++ for Dynamics AX because the data is in tables and invites the use of grouping, filters, and joins for much more complex data manipulation.

public void printMessage()

{ 

crazyLuckyTable  crazyTable; 

select answernumber, answer from crazyTable

   where crazyTable.answerNumber == luckyNumber; 

 Box::info (strFmt(crazyTable.answer),"Answer","This is the answer to your question"); 

}

While Statement

Use the while statement to repeat in a loop format over the contents of the table, in order to find the appropriate record since it may be unknown. This is much more likely in Dynamics AX programming because it may possibly be manipulating millions of records. This version of the while statement is called the do while. It executes the loop at least once. An alternative method is to use the while method that evaluates at the top of the equation, so it may not run. On a quick programming note, the counter variable needs to be declared in the class declaration, and the initial value is 0 (zero).

public void printMessage()

{ 

crazyLuckyTable  crazyTable; 

print "Lucky Number ";

print luckyNumber;

do

{

counter ++;

select answernumber, answer from crazyTable

where crazyTable.answernumber == counter;

}

while (counter!= luckyNumber);

Box::info (strFmt(crazyTable.answer), "Answer","This is the answer to your question");

Box::info (int2str(counter), "Tries","This is the number of records to find the correct one"); 

}

Conclusion

This article explored the key aspects of four X++ programming control statements in AX2012. Through the use of the Magic Eight Ball, you randomly selected a statement that improves business decision making with wisdom from beyond. The If statement shows the logic tree that can be used to evaluate a statement and assign a value or execute an operation. The switch statement explores much of the same information used in the if statement, only in less characters. It also illustrates how the values can be substituted with labels, increasing the capability for language translation. The select statement gives the opportunity to see the data retrieval that is possible. Finally, the while statement adds a looping structure for the review, update, or deletion of millions of records in a table. This is not a comprehensive approach to all that X++ programming uses for control statements, however, it describes the basics that enable further pursuit to working with Dynamics AX.

Comments

Comments are closed here.

Receive Posts by Email

Subscribe to the Microsoft Dynamics blog and receive notifications of new posts by email.