Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-03

Compare record

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Thompson
to aradeonas
aradeonas wrote
Hi, How can I compare two record?

I usually end up writing my own - as per the first answer here: http://stackoverflow.com/questions/11797069/how-to-compare-two-trect-variables-in-d7

I'd be interested in knowing if there was a more general method nowadays - though I doubt it.

Cheers

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150311/6ffaafed/attachment.html>

Reply by email

Jürgen Hestermann
to aradeonas: How can I compare two record? Like this : Regards, Ara http://www.fastmail.com - Email…
aradeonas wrote 15 lines
How can I compare two record? Like this : type TRec=record na:string; end; var R1,R2:TRec; Begin R1.na:='A'; R2.na:='B'; if R1R2 then Showmessage('Error')

In your example you would not need the record
if it consists of only one string component.
But you can write:

if R1.naR2.na then
    Showmessage('Error')

In general there cannot be a standard
compare function for records because
you can have a lot of different types
within a record. So you need to write
your own compare function.

Reply by email

Sven Barth
to aradeonas: How can I compare two record? Like this : Regards, Ara http://www.fastmail.com - Email…
aradeonas wrote 16 lines
Hi, How can I compare two record? Like this : type TRec=record na:string; end; var R1,R2:TRec; Begin R1.na:='A'; R2.na:='B'; if R1R2 then Showmessage('Error')

At the declaration of TRec add this:

=== code begin ===

operator = (aLeft, aRight: TRec): Boolean;

=== code end ===

and then implement it like this:

=== code begin ===

operator = (aLeft, aRight: TRec): Boolean;
begin
   Result := aLeft.na = aRight.na;
end;

=== code end ===

Alternatively you can declare the operator inside TRec if you enable modeswitch advancedrecords:

=== code begin ===
type
   TRec = record
     na: String;
     class operator = (aLeft, aRight: TRec): Boolean;
   end;

=== code end ===

Implementation:

=== code begin ===

class operator TRec.=(aLeft, aRight: TRec): Boolean;
begin
   Result := aLeft,na = aRight.na;
end;

=== code end ===

This /should/ work though I did not test it by compilation.

Regards,
Sven

Reply by email

aradeonas
to Sven Barth

Thank you J?rgen,That is just a example but Its good to know there is now way.
Thank you Sven for examples.I didn't know about advanced record support.

Regards,
Ara

Sven Barth wrote 70 lines
On 10.03.2015 22:23, aradeonas wrote: > Hi, > How can I compare two record? > Like this : > > type > TRec=record > na:string; > end; > > var > R1,R2:TRec; > Begin > R1.na:='A'; > R2.na:='B'; > if R1R2 then > Showmessage('Error') At the declaration of TRec add this: === code begin === operator = (aLeft, aRight: TRec): Boolean; === code end === and then implement it like this: === code begin === operator = (aLeft, aRight: TRec): Boolean; begin Result := aLeft.na = aRight.na; end; === code end === Alternatively you can declare the operator inside TRec if you enable modeswitch advancedrecords: === code begin === type TRec = record na: String; class operator = (aLeft, aRight: TRec): Boolean; end; === code end === Implementation: === code begin === class operator TRec.=(aLeft, aRight: TRec): Boolean; begin Result := aLeft,na = aRight.na; end; === code end === This /should/ work though I did not test it by compilation. Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - Choose from over 50 domains or use your own

Reply by email

Michael Schnell
to Mattias Gaertner
Mattias Gaertner wrote
"Alternatively you can declare the operator inside TRec if you enable modeswitch advancedrecords" It means: {$modeswitch advancedrecords}

Nice ! (I tested this but forgot the mode switch ;) )

Why is this mode not the default ?

-Michael

Reply by email