Search through blog..

Saturday, November 3, 2012

Few facts about Dynamics Ax Classes

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:
  1. Classes can be inherited
  2. A class method can refer to other methods within a class
Dynamics AX has two main categories of classes:
  1. Application classes - We use application classes for constructing our application. Only these classes can be created or modified.
  2. System classes - These are primarily used for doing runtime changes to the user interface.
Methods:
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.
  1. 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)
  2. Variable declaration:
    The first few lines of code in a method always contains the variable declaration, where we define variables needed in the method
  3. 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.
  4. 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.
Example of a method:
//Identification
public AmountMst sumCustTrans(CustAccount _custAccount, TransDate _transDate = systemdateget())
{
//Variable declaration
CustTrans custTrans;
AmountMst sumAmountMst;
;
//Code lines
if (!_custAccount)
throw error("Customer account not specified.");
sumAmountMst = (select sum(amountMst) from custTrans where custTrans.accountNum == _custAccount && custTrans.transDate >= _transDate).amountMst;
//return value
return sumAmountMst;
}
 
 
This method calculates the sum of the customer transaction and returns the sum using the variable sumAmountMst.
 
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.
 

An application class has 3 default nodes:
  1. Class Declaration:
    Variables can only be declared in Class declaration, we cannot initialize variables here |
  2. new() method:
    Invoking new() will call the constructor for the class. A constructor is used to initialize a class.
  3. 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
  1. 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.
  2. Check the left window to make sure that ClassDeclaration is selected. Rename the class by changing "Class1" to "MyFirstClass".
  3. In ClassDeclaration, declare a variable of the extended data type ItemId. ClassDeclaration should look like the following:
    class MyFirstClass
    {
    ItemId itemId;
    }
  4. 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".
  5. Set the parameter variable equal to the global class variable itemId. The method must return itemId.
    itemId parmItemId(ItemId _itemId = itemId)
    {
    ;
    itemId = _itemId;
    return itemId;
    }
  6. 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*.
  7. Click the save icon in the editor tool bar to save all changes to the class.
The easiest way of testing a class is by creating a job and printing the necessary variables to the Infolog. Example for the above class is:
Static void Classes_TestMyFirstClass(Args _args)
{
MyFirstClass myFirstClass;
;
myFirstClass = new MyFirstClass();
myFirstClass.parmItemId("Item100");
info(myFirstClass.parmItemId());
}

Output:
 
 

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
 
Access ModifierDescription
PublicThis 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.
ProtectedOnly methods can be protected.
A protected method can overridden in subclasses, but can only be used inside the class hierarchy.
PrivateBoth 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.

Post a Comment