Declaration of Classes
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
You create application classes by using the Application Object Tree (AOT).
Creating a Class in the Application Object Tree
You create an application class by following these steps:
In the AOT, locate the Classes node.
Right-click the Classes node and click New Class in the shortcut menu.
Expand the Class1 node, so you can see the classDeclaration node.
Right-click the classDeclaration node, and then click View Code. This displays the code Editor window.
In the classDeclaration code block, before the open brace {, the class declaration is decorated with the public modifier. All classes in Microsoft Dynamics AX are public. If you remove the public modifier, the system still treats the class as public.
Other modifiers can be specified on the class declaration, including final and extends.
Creating Variables in a Class
All classes are public, but all member variables are implicitlyprotected. Even though all member variables are protected, you cannot decorate a member variable with the private or public keywords. All member variables belong only to object instances of the class. You cannot decorate a member variable with the static keyword. The following steps exemplify these rules:
Right-click the classDeclaration node, and then click View Code.
In the code Editor window for the classDeclaration, add the following declaration of a string variable on a line between the open and close braces {}:
str firstName;
The X++ code for the classDeclaration now looks like the following.
public class Class1
{
str firstName;
}
You can provide access to a member variable by returning it from a simple instance public methods that get and set its value.
For information about how to write variable declarations, see Declaration of Variables.
Creating Accessor Methods in a Class
In this section you add simple public methods that get and set the value of the member variable firstName.
Add a Getter Accessor Method
Follow these steps to add a method that gets or returns the value of a member variable:
Right-click the Class1 node, and then click New > Method.
Rename the method1 node to getFirstName.
In the code Editor window, paste in the following X++ code for the method.
public str getFirstName()
{
return firstName;
}
> [!NOTE]
> <P>The this keyword cannot be used to reference member variables such as firstName.</P>
Add a Setter Accessor Method
Follow these steps to add a method that sets the value of a member variable:
Right-click the Class1 node, and then click New > Method.
Rename the method1 node to setFirstName.
In the code Editor window, paste in the following X++ code for the method.
public void setFirstName(str _firstName)
{
firstName = _firstName;
}
Test the Accessor Methods
The following X++ job tests the preceding getter and setter accessor methods.
static void AccessorJob61(Args _args)
{
Class1 c1;
c1 = new Class1(); // ‘new’ method is inherited from the Object class.
c1.setFirstName("Isaac");
print c1.getFirstName();
pause;
/*** Content of the Print window:
Isaac
***/
}
See also
Best Practices for Class Declarations
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.