Methods in X++

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The following blocks of code are standard for X++ application classes:

Code block type

Description

classDescription declaration block

Contains class modifiers such as public, private, and extends.

Also contains the field members for objects that are constructed from this class. IntelliSense can display a list of the members when you type the X++ keyword this.

new method

The constructor. This method can be called only by using the new keyword. The syntax is typically similar to:
 myClassInstance = new MyClass();

Derived classes can call the new method of their constructor by calling super method reference.

finalize method

The destructor method.

However, in X++ this is a destructor only by convention. The finalize method is not called automatically by the system during garbage collection.

Additional methods for a class fall into the following types:

  • Instance methods

  • Static methods

  • Main methods

Note

Methods can be created on many kinds of items in the AOT other than just classes. The list includes the following:

  • Maps

  • Views

  • Data Sets

  • Forms

  • Queries

Instance Methods

Instance methods, or object methods, are embedded in each object that is created from the class. They are called by using the following syntax:

objectHandleName.methodName();

You must instantiate the object before you can use the method.

Warning

If you later convert an instance method to a static method, you must restart the Microsoft Dynamics AX client for the compiler to note the change. Once you have converted the instance method to a static method, you can no longer call this method from the instance of the class -- only the class itself. Static methods are discussed in the next section.

Static Methods

Static methods, or class methods, belong to a class and are created by using the keyword static. They are called by using the following syntax:

ClassName::methodName();

You do not need to instantiate an object before you use static methods. Static methods are widely used in Microsoft Dynamics AX to work with data that is stored in tables.

Note

It is not possible to use member variables in a static method.

For more information, see Static Methods.

Main Methods

A main method is a class method that is executed directly from a menu option.

static void main (Args _args) 
 { 
     // Your X++ code here. 
 }

The method should only create an instance of the object and then call the necessary member methods. The _args parameter allows you to transfer data to the method.

For more information, see Activating a class from a menu item.

See also

Declaration of Methods

Method Modifiers

Overriding a Method

Best Practices for Methods

Table Methods

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.