X++ Control Statements in Microsoft Dynamics 365 F&O
1. Variables
Variables hold data when a block of code executes. Variables all have scope. The 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.
2. 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 the 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
3. 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 AX/365:
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.
4. 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.
5. 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:
5.1 Array
Arrays can be declared by adding brackets ([ ] ). You can set the maximum number of array elements by putting the number in brackets.
Array values can be set by specifying the index when assigning the value.
5.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.
There are many functions that manipulate container variables. The following functions are available:
The following examples have a container variable that contains four values, including three different data types.
6. 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.
6.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.
6.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:
Comments
Post a Comment