Comparing Objects

Suppose I declare a class TTest

TTest=class(TObject)
             fred: string;
             chris: boolean;
             end;

Then declare and create 2 objects: test1 and test2

var
   test1, test2: TTest;

procedure TForm1.Button1Click(Sender: TObject);
begin
  test1:= TTest.create;
  test2:= TTest.create;
end;

Then later I want to compare the values of the parameters of each class to see if they match. I dont want to have to compare each parameter like this:

  if (test1.fred=test2.fred) AND (test1.chris=test2.chris) then...
 
This gets very messy if the object has 10 or more parameters to compare.

Ideally I would like to do something like

if test1 = test2 then...

but this doesn't work!

Any suggestions
LVL 2
aubsAsked:
Who is Participating?
 
MadshiConnect With a Mentor Commented:
Ahh - another try...   :-)
0
 
cabelaCommented:
The simple comparison does not work because you are dealing with pointers. You have to compare the objects. You can do a call like the following:

CompareMem (P1, P2, P1.InstanceSize)

But this works only if you do not have other pointers in the object (like string). To compare these, you have to compare them separatly.
0
 
MadshiCommented:
You can write a function like this
 
type
  TTest=class(TObject)
    fred  : string;
    chris : boolean;
    function IsEqual(otherTest: TTest) : boolean;
  end;

function TTest.IsEqual(otherTest: TTest) : boolean;
begin
  result := (fred  = otherTest.fred) and
            (chris = otherTest.chris);
end;

Now you can compare two test objects like this:

  if test1.IsEqual(test2) then
    ...

Regards, Madshi.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
aubsAuthor Commented:
Hi Cabela

I realised that the reason

  if  test1=test2...

doesnt work is because you're comparing everything in each object. I had already tried something similar to your suggestion with pointers but still had no success.

Basically what your saying is that I'm stuck with the long winded method of:

  if (test1.fred=test2.fred) AND (test1.chris=test2.chris) AND ...

If this is true then I think that Madshi's suggestion is a good one.
0
 
cabelaCommented:
Yeah, Madshi's idea is a nice and simple one. And moreover it's safe. Not like mine.
0
 
MadshiCommented:
Thanx...  :-)  Well, I'm using my suggestion in my own sources, too.
0
 
ptmcompCommented:
Sometimes you can deal with records:
TMyRec= packed record
  Byte1, Byte2: Byte;
  Word1: Word;
  str: ShortString;
end;

TMyClass= class
private
  FMyRec: TMyRec
end;
Now you can compare the memory of the FMyRecs but like cabela said that doesn't work with long strings.
ptm.
0
 
aubsAuthor Commented:
OK Madshi,

Can you post an answer so that I can give you the points.

Aubs
0
 
cabelaCommented:
You can do this by accepting a comment as an answer....
0
 
MadshiCommented:
Right...  :-)  You could have accepted one of my comments as an answer - anyway, here is the answer...

Regards, Madshi.
0
All Courses

From novice to tech pro — start learning today.