[Lazarus] Compare record
Sven Barth
pascaldragon at googlemail.com
Wed Mar 11 07:43:21 CET 2015
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 R1<>R2 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
More information about the Lazarus
mailing list