Dynamics 365 FO - X++ Control Statement - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Thursday, September 15, 2022

Dynamics 365 FO - X++ Control Statement

 Dynamics 365 FO - X++ Control Statement

Nghia Song -  Microsoft Dynamics 365 Technical Consultant

Nghia Song

Tel - WhatsApp: +84967324794

Email: songnghia.uit@gmail.com


  1. Objects

The objectives are: 

  • Declare and use extended data types for variables. 

  • Use the various operators available in X++. 

  • Control program flow using conditional statements in X++. 

  • Repetitively call the same blocks of code by using Loop statements. 

  • Use standard functions that are built in to the application. 

  • Use output commands to display data and messages to the user.

  1. Introduction

This course explains how to use control statements in X++. These statements control the logic flow in the program. This course also describes how to use some built-in functions in Microsoft Dynamics AX to communicate with the end-user.

  1. Variables

Variables hold data when a block of code executes. Variables all have scope. Scope is the area where the variable can be accessed. Some different types of scope include:

BEST PRACTICE: Do not use names like string1. Always give a variable a meaningful name so its use is easier to identify when reading the code.

  1. Declaration

All variables must be declared before use. When a variable is declared, a small amount of memory is reserved. The syntax of the declaration is the same, whether it is a simple variable or an object variable. 

You cannot mix variable declarations with other X++ statements. Variables must be declared before the statements. 

There are two rules to use when declaring variables: 

  • Declare all variables before anything else in the code. 

  • Use a semicolon after each declaration.

  • You might want a variable to have a value other than the default when the variable is declared. X++ supports initialization of variables in the Declaration statement. Initialization is performed by adding the assignment-statement to the variable declaration. For example:

int a = 10; // a is assigned the value 10

  1. Simple Data Types

X++ implements primitive data types that can be used to declare variables in 

X++ code. You can also create Extended Data Types on the basis of primitive types

The following table provides an overview of the simple data types available in Microsoft Dynamics 365:

The syntax for the simple variable declaration is as follows:

The data type can be any of the data types in X++. Here are some examples:

When declaring a variable, you can also declare them as an extended data type. 

This is a good practice because it can highlight errors in code at compile time when a variable is used incorrectly.

It is common to name the variable the same as the extended data type, when possible.

  1. Initializing Variables

The following statements show how to declare the variables for use later in the code. You can declare several variables of the same type with different names. You can also assign a value to the variable when you declare it or later in the code.

  1. Composite Data Types

In addition to the simple data types, you can use composite data types when declaring variables. 

The following composite data types are available:

7.1 Array

Arrays can be declared by adding brackets ([ ] ). You can set the maximum number of array elements by putting the number in the brackets. 

Array values can be set by specifying the index when assigning the value.

7.2 Containers

A container variable can contain different types and values of simple and extended data types, including arrays and other container variables. Classes cannot be put into containers. 

Many functions manipulate container variables. The following functions are available:

The following examples have a container variable that contains four values, including three different data types.

  1. Operators

Operators are used to manipulate variable and field values and to control the logical program flow based on the values in variables and fields. The following types of operators are available. 

  • Assignment operators modify the contents of a variable or field. 

  • Arithmetic operators perform mathematical operations on the values in a variable or field. 

  • Relational operators evaluate how two values relate to one another and return either True or False according to the result.

8.1 Arithmetic Operators

Arithmetic operators perform calculations in X++. Arithmetic operators are used like relational operators except for '~, ++, --, +=, and -='. The following table defines available arithmetic operators:

The following are some examples of these arithmetic operators. For all examples, the variable 'i' is an integer.

8.2 Relational Operators

Relational operators, except for '!', are placed between two expressions. The following table defines available relational operators:

The following are examples using relational operators and their return values:

  1. Conditional Statements

Conditional statements in programming define conditions under which certain functions are performed. Conditional statements use logical expressions that are evaluated and return a value of either true or false. There are three primary conditional statements: 

  • If statement 

  • Switch statement 

  • Ternary operators 

All these statements are evaluated using operators.

9.1 If

The if statement is the simplest control statement. It checks whether a condition is true or false. If the condition is satisfied, all the code within the braces '{}' is executed. The syntax for an if statement is as follows:

The following is an example of an if statement. If the variable a is greater than 10, the value of a will be printed to the screen.

The following is an example of an if statement using multiple expressions to evaluate the condition.

9.2 If…else

An if statement checks for only one possibility and ignores all other conditions. An if…else statement checks one condition and if true, the block of code is executed. Otherwise, an alternative statement is executed. The syntax for an if…else statement is as follows:

The previous conditional formulas allow for only two alternative outcomes. A program might have to check more than two alternatives. To check for multiple alternatives, you can use an if…else...if statement. The syntax for this statement is as follows:

9.3 Ternary Operator

This conditional statement behaves exactly like an if…else statement. The main reason to use the ternary operator is convenience in coding. Its syntax is as follows:

The condition is checked first and if true, statement 1 is executed, if false, statement 2 is executed. However, the two expressions following the question mark (?) must be of the same data type.

This example using the ternary operator, is equivalent in logic and results to the previous example using the if...else statement.

9.4 Switch

A switch statement acts as a multi-branch control statement that defines an expression and whose result leads to a specific program execution. The switch statement considers the result and executes code, depending on possible outcomes of the expression. These are known as cases. Each of these cases is listed in the body of the statement.

Following the colon after each case are statements that execute if the expression satisfies the case. There can be any number of statements following a case in a switch statement. The body of a switch statement is enclosed in braces '{}'. The following shows the syntax for a switch statement:

Example

  1. Loops

Repetitive statements, also known as loops, conditionally control data input and output. There are three main loops in X++: 

  • While loop 

  • Do while loop 

  •  For statement

  1. While

The while loop evaluates a condition and executes statements, depending on whether the condition is satisfied. The loop continues to check the condition, and as long as the condition is true, it continues to execute the statements. As soon as the condition becomes false, the statement is exited. The syntax is as follows:

This is a simple while statement that counts from 1 to 5

HINT: The previous example uses the counter++which can increment any integer by 1. You can also set the increment to any value by using the variable+=<# to increment>syntax. For example: counter+=2; //This increments the counter variable by two every time

Notice the condition is evaluated before the statements are executed. In the previous example, it means that if the counter variable is greater than five when it reaches this loop, it does not execute. Therefore, a while statement can be executed zero or more times.

  1. Do..while

The function of a do while statement is almost identical to the while statement. The main difference is that the condition is evaluated after the statement executes. The effect is that the loop always runs at least one time. The following is the syntax for a do while statement:

  1. For

The for statement uses an incrementing counter variable, and a condition, to determine the how long it will continue to loop. The parameters in a for statement include three elements: 

  • The initial value of the variable. 

  • The condition under which the loop will continue. 

  • The amount the variable is increased or decreased by. 

  • The syntax can be defined as follows:

For loops are frequently used when navigating through an array. The following is an example of how to use a for loop to print each element of a string array called 'abc' that contains 10 strings:

A for loop and a while loop perform the same function. However, the for loop is more condensed in structure. In a while loop, if the counter variable is not incremented, an infinite loop can occur. This is not possible with a for loop because you receive a syntax error if that part of the condition is not qualified.

  1. Continue and Break Statements

You can use continue and break statements within all three loops to tell the execution to break or continue. 

The break statement completely interrupts a loop at any time. The best example of a break is in a switch statement.

The continue statement ignores the rest of the current loop and continues to control the loop. The following example uses a for loop with the continue statement to control which values print to the screen. The loop executes the print statement for values <= 3 and >= 8


  1. Build-In Functions

Microsoft Dynamics 365 contains many built-in functions to help in X++ development. These functions perform mathematical operations, convert data types, return system values, and so on. Built-in functions can be used anywhere in X++ code. These functions can be typed manually or accessed by using the context (right-click) menu in the Code Editor and selecting List Built-in Functions, or by pressing Shift+F4.

  1. Communication tools

Communicating with the end-user is important and critical for an application to run correctly. The main types of communication include: 

  • Forms and reports used for input and output of larger amounts of data. 

  • Print statements, Infolog and dialog boxes generally used for specific data input and output. 

This lesson discusses how to use the print statement, create dialog boxes and Infolog. Forms and reports are covered in more detail in the next development course.

  1. Print…Pause

When the compiler reaches a print statement, it returns whatever value or variable value that immediately follows the print syntax. The following is an example of the print statement:

The pause line freezes the output text on the screen after the code runs so that it is easier to read. The pause statement causes a message box to pop up. It lets the user continue or end code execution. 

The print statement should not be used within the actual application business logic. It is used only as a programmers tool to display data or values while developing and debugging code.

  1. Boxes

Boxes display brief messages to application users. There are many box types and each has their own box method. 

Methods in the box class take the following parameters: 

  • The main text 

  • The title bar text 

  • Help text

The following is an example of an information box and how the parameters are used:

The warning box is used to display warning messages. The following example shows how the warning message is used:

  1. Infolog 

The Infolog is the most common method used to communicate with the user about how a process is executed. Boxes can output a message to a user. However, sometimes multiple messages are generated during processing. Infologs are more suited to handle multiple messages; there are three types of messages:

  1. Dialog boxes

Dialog boxes are a simplified type of form in Microsoft Dynamics 365, and they are generated from the Dialog class. They create a dialog with the user where the user can input values. Dialog boxes are not used in complex scenarios; forms are the preferred

The method in complex situations. All dialog boxes have a standardized form that contains an OK/Cancel option.

The dialog.run()method returns true if OK is clicked, and false if Cancel is clicked.

No comments:

Post a Comment