Constructors [AX 2012]
Updated: November 30, 2011
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
To create an instance of a class (an object), you have to instantiate it. The default constructor is the new method:
// Declares a variable to refer to a Point object
// Allocates an instance of a Point object
myPoint = new Point();
It is a best practice to make the new method protected, and instead, use a static construct method, or static new method as the public constructor for the class. If no initialization is required, use a static construct method, otherwise use a static new method. For more information, see Best Practices for Constructors.
A class constructor can instantiate other objects, in addition to creating an instance of the class. The following code illustrates one such situation by declaring a Rectangle class that uses two Point objects to define its bounds.
X++ does not allow you to access the variables of an object by referring to the names of the variables, qualified with the name of the object:
myPoint.x = 10.0; //Not possible
myPoint.y = 25.7; //Not possible
The only way to access the member variables in a class is through accessor methods. For information about accessor methods, see Accessor Methods.
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.