Thursday 24 May 2018

Access modifiers in X++ Language (AX2012 & D365)


There are several access modifiers that can be applied to method declarations. Some of the modifiers can be combined (for example, final static). Some modifiers can only be used methods, while others can be used on classes or methods.

The following list describes the method modifier keywords of the X++ language.

Abstract: The method is declared but not implemented in a parent class. The method must be overridden in sub classes. If you try to create an object from a subclass where one or more of the abstract methods belonging to the parent class have not been overridden, you will get a compiler error.

Note: Classes can also be abstract. Sometimes a class represents an abstract concept, but it should not be instantiated: Only sub classes should be instantiated. Such base classes can be declared abstract. Consider the case where the programmer wants to model the concept of an account. Accounts are abstract—only derived classes (ledger accounts and so on) exist in the real world. This would be a clear case for declaring the Account class abstract.

Final: Indicates that the method cannot be overridden in any class that derives from its class.

Public: Methods that are declared as public are accessible anywhere the class is accessible and can be overridden by sub classes. Methods that have no access modifier are implicitly public.

Protected: Methods that are declared as protected can only be called from methods in the class and in sub classes that extend the class where the method is declared.

Private: Methods that are declared as private can be called only from methods in the class where the private method is declared.

Client: Establishes the location where the method is to be executed (on the client). Can only be used on static methods. If the method is not static, specify the location by using the class property RunOn.

Server: Establishes the location where the method is to be executed (on the server). Can only be used on static methods. If the method is not static, you need to specify the location using the class property RunOn.

Display: Indicates that the method's return value is to be displayed on a form or a report. The value cannot be altered in the form or report. The return value is typically a calculated value, for example, a sum.

Edit: Indicates that the method's return type is to be used to provide information for a field that is used in a form. The value in the field can be edited.

Static: Specifies that the method is a class method and does not operate on an object. Static methods cannot refer to instance variables and are invoked by using the class name rather than on an instance of the class (MyClass::aStaticMethod()).

No comments:

Post a Comment

How to fix Error: "Common Language Runtime detected an invalid program" in D365FO

Recently, we upgraded our environment, which resulted in one of our customizations, previously working flawlessly, throwing the "Common...