Dynamics 365 FO - Objects and Classes
Nghia Song - Microsoft Dynamics 365 Technical Consultant
Nghia Song
Tel - WhatsApp: +84967324794
Email: songnghia.uit@gmail.com
Objects and Classes
Objects
• Use the classes within Microsoft Dynamics 365 X++ development.
• Control access to methods using Access Control Method Modifiers.
• Extend a class using the concept of inheritance.
• Describe the differences between an object and a class.
• Initialize variables in the appropriate place according to scoping rules.
• Call methods within the same class.
• Use the different method types available.
• Describe the similarities and differences between tables and classes.
• Use the eventing publisher and subscriber model when modifying code in the application.
Classes
A class is constructed of members which can be variables or methods.
Variablesare used to store the data for the class. They are specific to an object; every object instantiated from the class declaration has its own copy of the variable. Such variables are known as instance variables.
Methods are the functions that operate on the data and define the object's behavior. They are often declared to operate on the instance variables of the class and are known as instance methods.
A class is not an object. A class is an outline that defines how an object behaves when the object is created from the class. Obtain concrete objects by instantiating a previously defined class. Many objects can be instantiated from one class definition.
A class contains the properties, methods, and variables needed by the object. Create a class by right-clicking the Classes node of the AOT and selecting New Class, or by using the Class Wizard at Tools> Development Tools> Wizards.A class always contains a classDeclaration node and two methods:
ClassDeclaration: Specifies the name of the class and necessary variables. It can also specify inheritance.
NOTE: classDeclaration is not a method. It is a class-level scope area used to define variables that are global to all non-static methods within the class. This means that you can use these variables in any methods in the class, and the variable value will be the same in each method.
New(): This method instantiates a new object. You can also assign values to object variables using this method.
Finalize():This method terminates the object. It is never used within the application and exists only for convention.
These methods enable the object instantiation to occur. An infinite number of methods can be added to a class.
Method and access control
Methods in a class are always available to other methods in the class. However they should not always be available tomethods in other classes. To control access to a method, a methods modifier is placed in the method definition.
Modifiers
There are three modifiers available.
Public allows the method to be called from any code in the application.
Protected allows the method to be called only by methods in the same class or subclasses of the class in which the method is defined.
Private allows the method to be called only by methods in the same class in which the method is defined.
When a new method is created, the default modifier of the method is Private.
Inheritance
Inheritance is a concept where one class can inherit all the methods and variables from another class. A child class inherits the methods of the parent class. Additional methods can be applied to the child class, and inherited methods can be overridden. This means the child class is not always identical to the parent class.
The advantage of inheritance in object-oriented programming is that code can be written one time and be reused many times.
NOTE: In X++ one class can extend another class, and one extended data type can extend another.
Extend a Class
To have a class extend another class, modify the classDeclaration code of the child class by adding extends and then the parent class, shown as follows:
Objects created from the Child
Have at least the same methods and variables as the Parent.
Can have methods and variables that do not exist in the Parent.
Can have methods from the Parent that are overridden or altered, in the Child.
For example, if the Parent contains four methods:
Method1, Method2, Method3, Method4
The Child can have two of those methods overridden, plus an additional method.
Method1, Method3, Method5
If the code refers to one of the methods in the Child that has not been overridden, the system will go to the Parent to retrieve the method. If that method then calls a method that is in the Child, it will revert back to the Child.
Objects
Objects are created at run-time using the classes defined in the AOT. To create an object from a class, the class has to be instantiated.
Instantiating a class
Object instance methods can only be used when an object is instantiated from a specific class. To instantiate a class is to create a new instance of it. You can have multiple instances of a class, meaning, you can have the same code running multiple times. Think of how in Microsoft Word, you can have two documents open at the same time. They are both running the same program, but are two separate instances of that program. When you create a new object, call the new() method to execute the code when the object is instantiated.
Once the class is instantiated, to execute a specific method from your code, use the reference variable followed by a "dot" operator then the method name.
Scope and parameters in X++
Instance variables declared in class declarations, can be accessed from any methods in the class, and from methods in sub-classes that extend the class.
Local variables can be accessed only in the method in which they are defined.
Methods with Parameters
All methods in X++ have their own scope. To use data from a different scope, transfer the data into the new scope using parameters.
A method can have one or more parameters. Within the scope of the method, these parameters are treated like local variables, initialized with the value from the parameter in the method call.
All parameters are passed by value, this means you cannot change the value of the original variable, but only the local variable in the method, which is a copy of the original.
In the previous example, the second parameter is assigned a default value. This means that when the method is called, the second parameter is optional. If it is called without a second parameter,_parm2 will be set to 1.
Methods returning data
When you right-click a class in the AOT and select New method, the method is declared with the keyword Void. The void (empty) specifies that the method does not return a value.
When enabling a method to return a value, remember two things:
Replace the void with an Extended Data Type to specify the type of data to receive from the method. Any kind of data can be returned including table buffers and class objects.
Write return followed by a specification of the data that you want to receive for any manipulation of the data.
Referencing object methods
It is common to call methods within one class from other methods in that class. To refer to a method in the same class, use the keyword this.
Example of Using This:
A class has a method called myMethod() and another called run(). The run() method calls the myMethod() method.
Method types
Static methods
Static methods are attached to a class. However, they do not need that class to be instantiated to execute that method. They are not within the scope of the class, so any class variables are not available in a static method.
Static methods are declared static by using the Static method modifier.
Static methods are called using the class name followed by two colons (::) and then the methods name.
The following example shows a static method declaration and a call to that static method.
Main method
The main method is a static method that can be used to call a constructor. It is special because its name is required to be "main". It is used by the system when the class is run directly from a menu item and it takes a parameter of type args.
Args is a class that is used to pass parameters between objects, for instance, various parameters can be set on the properties of a menu item. When the menu item calls a class, the args class containing those property values is passed to the main method using the args parameter.
Display methods
Display methods are used on forms. They are commonly created on the table, and can also be defined on the form. Display methods return a value that is displayed on the form or report. They are often used to display a calculation, or to look up a single field from another table. The following example shows how to display the item name on a form that displays data from a table containing the item id.
Accessor methods
Accessor methods enable other elements to set or get the values of variables in a class and, commonly, they do both.
The following example accepts a value as a parameter, sets a class variable to this value and then returns the value. The parameter has a default value set to the class variable value, so if the method is called without a parameter, it returns the value of the class variable. If it is called with a value in the parameter, then the class variable is set to this value.
Tables as classes
A table can be considered an independent class used to address fields or methods defined on that table. In fact, when a table is instantiated, it is done so with the system class called xRecord. This class contains methods called when committing record changes to the database and some other system methods that operate on records.
Differences between tables and classes include the following:
A place for a table buffer is automatically assigned in a table (in classes the new method is used).
Fields in tables are public; they can be referred to from everywhere.
Fields in tables can be referred to directly; for example, in a report, whereas variables in a method can only be referred to using accessor methods.
Table Code
No comments:
Post a Comment