Value based comparison in X++ D365O - X++ objects equality

Rumman Ansari   Software Engineer     379 Share
☰ Table of Contents

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:

  1. Class Definition: The code starts by defining a class named VehicleClass.

  2. Instance Fields: Inside the class, there are two instance fields: height and width. These fields represent properties or characteristics of a vehicle object, such as its dimensions.

  3. Constructor: The class includes a constructor (new) that initializes the height and width fields with the values passed as parameters.

  4. Main Method: The public static void main(Args arg) method is the entry point for the program. It creates two instances of the VehicleClass class, obj and obj1, both with the same height and width values.

  5. Custom Equals Method: The class defines a custom Equals method. In many programming languages, including C# and Java, the Equals method is used to compare objects for equality. The custom Equals method in this code follows a specific pattern for comparison:

    • It first checks if the _obj parameter is null and returns false if it is because you cannot compare with a null object.

    • It checks if this and _obj refer to the same object in memory using the System.Object::ReferenceEquals method. If they do, it returns true, indicating that the two objects are the same.

    • It compares the types of this and _obj. If they are not the same type, it returns false because different types are not considered equal.

    • If the types are the same, it proceeds to compare the height and width fields of the two objects. If they have the same values, it returns true, indicating that the objects are equal. Otherwise, it returns false.

  6. Main Method (Continued): In the main method, it calls the custom Equals method to compare obj and obj1 and stores the result in the returnValue variable. It then uses the info 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 heightandwidthvalues ofobjandobj1are the same, the output will be "true," indicating that the objects are equal according to the customEquals` method. Otherwise, it will be "Width false," indicating that the objects are not equal.