X++ is an Object Oriented Programming language and a class an important part of X++ langauge. A class can be defined as a collection of methods. Compared to table methods, using a methods in class makes reusing your code easier and this is because of the below two reasons:
- Classes can be inherited
- A class method can refer to other methods within a class
Dynamics AX has two main categories of classes:
- Application classes - We use application classes for constructing our application. Only these classes can be created or modified.
- System classes - These are primarily used for doing runtime changes to the user interface.
Classes are divided into methods, where each method should be doing a single task. The code in a method consists of 4 blocks: Identification, variable declaration, code lines and return value.
- Identification:In the top of a method is the identification of the method which has the following sytax:<modifiers> <return type> <method name> (Parameter profile)
- Variable declaration:The first few lines of code in a method always contains the variable declaration, where we define variables needed in the method
- Code lines:Code lines lie right below the variable declaration code. These code lines determine the behavior of the method. This is where business logic is written.
- Return value:This is the value we are intending to return to the caller of the method. Calling return will end the execution of the method and value specified is returned back.
public AmountMst sumCustTrans(CustAccount _custAccount, TransDate _transDate = systemdateget())
throw error("Customer account not specified.");
sumAmountMst = (select sum(amountMst) from custTrans where custTrans.accountNum == _custAccount && custTrans.transDate >= _transDate).amountMst;
Like all other objects in Dynamics AX, Classes are listed in the Application Object Tree (AOT). And within them are listed methods. The icons for each method in a class are determined by the modifiers, which makes it easy to get an overview of the methods of a table or class. (say, CustBillOfExchangeClose for example)
Before being able to reference the class object, you must initiate the class object. Keyword new is used o initiate the class. And the class initiating the class object must be of the same type.
Syntax: <Class name> _objVariable = new <Class name>()
The constructors practice is not used in MorphX as new() is used as the constructor.
- Class Declaration:
Variables can only be declared in Class declaration, we cannot initialize variables here |
- new() method:
Invoking new() will call the constructor for the class. A constructor is used to initialize a class.
- finalize() method:
The method finalize() is used to remove the class from memory. This is not a method which is called automatically as the garbage collector will automatically remove objects not used anymore from memory.
The below is the simple flow to create and use a class
- Go to the node Classes, right-click and chooses New Class. A new class called "Class1" will be created. Open the class by double-clicking the new class.
- Check the left window to make sure that ClassDeclaration is selected. Rename the class by changing "Class1" to "MyFirstClass".
- In ClassDeclaration, declare a variable of the extended data type ItemId. ClassDeclaration should look like the following:
- Now add an new method to the class by pressing ctrl+n. The new method called "Method1" will automatically be opened in the editor. Change the name of the method to "parmItemId".
- Set the parameter variable equal to the global class variable itemId. The method must return itemId.
itemId parmItemId(ItemId _itemId = itemId)
itemId = _itemId;
- As class variables cannot be referenced from outside the class, so you can create methods like this to set and get the values of a class variable. Such methods are often prefixed with parm*.
- Click the save icon in the editor tool bar to save all changes to the class.
Static void Classes_TestMyFirstClass(Args _args)
myFirstClass = new MyFirstClass();
Modifiers: are quite useful as you can set restrictions on the use of a method or a whole class and how a class is inherited
|Public||This is the default behavior for classes and methods. |
A public class can be inherited and class methods can be overriden in subclasses and called outside the class.
|Protected||Only methods can be protected. |
A protected method can overridden in subclasses, but can only be used inside the class hierarchy.
|Private||Both classes and methods can be set as private. However this will only affects methods. A private method can only be used within the current class.|