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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
MadshiCommented:
Ahh - another try...   :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.