Type

TAnyObject = class private

Value: Integer; name: string; publ ic constructor Create (aName: string; aValue: Integer); function Equals(obj: TObject): Boolean; override; function ToString: string; override; end;

In the implementation of the three methods I simply had to change a call to GetType with that to ClassType:

constructor TAnyObject.Create(aName: string;

aValue: Integer); begi n inherited Create; name := aName; Value := aValue; end;

function TAnyObject.Equa1s(obj: TObject): Boolean; begi n

Result := (obj.ClassType = self.ClassType) and ((obj as TAnyObject).Value = self.Value);

end;

function TAnyObject.ToString: string; begi n

Notice that objects are considered equal if they are of the same exact class and their value matches, while their string representation includes only the name field. The program creates some objects of this class as its starts, associating them with the elements of two combo boxes:

procedure TFormSystemObject.FormCreate(Sender: TObject); begi n aol := TAnyObject.Create ('aol', l0);

ao4 := TAnyObject.Create ('ao4', 20);

  1. Items.AddObject (aol.ToString, aol);
  2. Items.AddObject (ao2.ToString, ao2);
  3. Items.AddObject (ao3.ToString, ao3);
  4. Items.AddObject (ao4.ToString, ao4);
  5. Items.AddObject (aol.ToString, aol); ComboBox2.Items.AddObject (ao2.ToString, ao2); ComboBox2.Items.AddObject (ao3.ToString, ao3); ComboBox2.Items.AddObject (ao4.ToString, ao4); end;

Notice that two references (ao2 and ao3) point to the same object in memory, and that the last object (ao4) has the same numerical value. As you select items in both combo boxes, you can compare the objects you have selected, both using equals and doing a direct reference comparison:

procedure TFormSystemObject.btnCompareClick(

Sender: TObject); begi n

Log ( 'Comparing ' +

  1. Items [ComboBoxl.ItemIndex] + ' and ' +
  2. Items [ComboBox2.ItemIndex]); Log ( 'Equals: ' + BoolToStr (
  3. Items.Objects [ComboBoxl.ItemIndex].Equals (
  4. Items.Objects [ComboBox2.ItemIndex]), True));

ComboBoxl.Items.Objects [ComboBoxl.Itemlndex] = ComboBox2.Items.Objects [ComboBox2.Itemlndex], True));

Here are some of the results:

Comparing aol and ao4 Equals: False Reference = False Comparing ao2 or ao3 and Equals: True Reference =: True Comparing ao2 or ao3 and Equals: True Reference =: False

The program has another button used to test some of these methods for the button itself:

procedure TFormSystemObject.btnTestClick(

Sender: TObject); var btn2: TButton; begi n btn2 := btnTest; Log ( 'Equals: ' +

BoolToStr (btnTest.Equals (btn2), True)); Log ( 'Reference = ' +

BoolToStr (btnTest = btn2, True)); Log ( 'GetHashCode: ' +

IntToStr (btnTest.GetHashCode)); Log ('ToString: ' + btnTest.ToString); end ;

The output is the following (with a hash value that might change upon execution):

Equals: True Reference = True GetHashCode: 28253904 ToString: TButton ao2 or ao3 ao4

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment