Value based comparison in X++ D365O - X++ objects equality
Table of Content:
Value based comparison in X++ D365O - X++ objects equality
class VehicleClass { // Instance fields. real height; real width; // Constructor to initialize fields height and width. void new(real _height, real _width) { height = _height; width = _width; } public static void main(Args arg) { VehicleClass obj = new VehicleClass(12.0, 12.0); VehicleClass obj1 = new VehicleClass(12.0, 12.0); anytype returnValue = obj.Equals(obj1); info(strFmt("%1", returnValue )); //info(strFmt("number Of Passengers %1", obj.numberOfPassengers)); } public boolean Equals(System.Object _obj) { if (_obj == null) { return false; } if (System.Object::ReferenceEquals(this, _obj)) { return true; } if (!this.GetType().Equals(_obj.GetType())) { return false; } VehicleClass compareTo = _obj; if (compareTo.height == this.height && compareTo.width == this.width) { return true; } else { return false; } } }
Above code defines a class called VehicleClass
and demonstrates how to use a custom Equals
method to compare instances of this class. Let's break down the code step by step:
-
Class Definition: The code starts by defining a class named
VehicleClass
. -
Instance Fields: Inside the class, there are two instance fields:
height
andwidth
. These fields represent properties or characteristics of a vehicle object, such as its dimensions. -
Constructor: The class includes a constructor (
new
) that initializes theheight
andwidth
fields with the values passed as parameters. -
Main Method: The
public static void main(Args arg)
method is the entry point for the program. It creates two instances of theVehicleClass
class,obj
andobj1
, both with the same height and width values. -
Custom Equals Method: The class defines a custom
Equals
method. In many programming languages, including C# and Java, theEquals
method is used to compare objects for equality. The customEquals
method in this code follows a specific pattern for comparison:-
It first checks if the
_obj
parameter isnull
and returnsfalse
if it is because you cannot compare with anull
object. -
It checks if
this
and_obj
refer to the same object in memory using theSystem.Object::ReferenceEquals
method. If they do, it returnstrue
, indicating that the two objects are the same. -
It compares the types of
this
and_obj
. If they are not the same type, it returnsfalse
because different types are not considered equal. -
If the types are the same, it proceeds to compare the
height
andwidth
fields of the two objects. If they have the same values, it returnstrue
, indicating that the objects are equal. Otherwise, it returnsfalse
.
-
-
Main Method (Continued): In the
main
method, it calls the customEquals
method to compareobj
andobj1
and stores the result in thereturnValue
variable. It then uses theinfo
method to display whether the objects are equal based on the custom comparison logic.
The output of this code will depend on the specific attributes and values of obj
and obj1. If the
heightand
widthvalues of
objand
obj1are the same, the output will be "true," indicating that the objects are equal according to the custom
Equals` method. Otherwise, it will be "Width false," indicating that the objects are not equal.