Hi,
How can I compare two record? Like this :
Show quoted text 7 lines
Regards, Ara

Mailing lists / lazarus / 2015-03
11 messages · 6 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
How can I compare two record? Like this :
Regards, Ara
aradeonas wroteHi, 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>
How can I compare two record? Like this : Regards, Ara http://www.fastmail.com - Email…
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.
How can I compare two record? Like this : Regards, Ara http://www.fastmail.com - Email…
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
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
aradeonas wroteoperator = (aLeft, aRight: TRec): Boolean;
I get "Logical Operator not supported".
-Michael
Michael Schnell wroteI get "Logical Operator not supported".
I do have:
{$mode objfpc}{$H+}I get "Logical Operator not supported". -Michael
Michael Schnell wroteI get "Logical Operator not supported".
I found that seemingly overloading an operator is not allowed within a procedure, while defining a type is allowed.
Is this a bug or a feature ?
Anyway, the error message is a bit misleading....
-Michael
Thank you J?rgen,That is just a example but Its good to know there is now way. Thank you…
aradeonas wroteoperator = (aLeft, aRight: TRec): Boolean;
Seemingly with "operator =", "operator " is automatically overloaded. Nonetheless it is possible to do your own (contradicting) "operator " if you like :-) .
-Michael
I get "Logical Operator not supported". -Michael
As Sven wrote:
"Alternatively you can declare the operator inside TRec if you enable modeswitch advancedrecords"
It means:
{$modeswitch advancedrecords}Mattias
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