10

It seems that one, in my opinion mandatory method is missing from TValue; TValue.Equals(TValue).

So whats a fast and decent way of comparing 2 TValues, preferably without the use of TValue.ToString(), which allows false matches between variants, records, etc.

Marius
  • 3,043
  • 1
  • 15
  • 24
  • I think DeHL (now discontinued) has some some stuff to do this. Don't know specifics. http://code.google.com/p/delphilhlplib/ – awmross Mar 21 '12 at 23:27

1 Answers1

14

Delphi-Mocks presents two functions :

function CompareValue(const Left,Right : TValue): Integer;
function SameValue(const Left, Right: TValue): Boolean;

With the record helper for TValue you can also do TValue.Equals(TValue);

Licensed under Apache terms and under permission by Stefan Glienke.

Here is the original source by Stefan : delphisorcery.

If you need to extend the functionality for variants, add:

function TValueHelper.IsVariant: Boolean;
begin
  Result := TypeInfo = System.TypeInfo(Variant);
end;

and insert

if Left.IsVariant and Right.IsVariant then
begin
  Result := Left.AsVariant = Right.AsVariant;
end else

after the isString comparison in the SameValue function.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • Upvote for the link, but it seems its still incomplete (for instance, determining the equality of variants is missing). I am looking for a smaller version, comparing e.g. memory adresses. – Marius Mar 21 '12 at 08:54
  • 1
    @Marius You can simply add the missing functionality yourself. – David Heffernan Mar 21 '12 at 09:31
  • @DavidHeffernan True, though that would render my initial question obsolete :p – Marius Mar 21 '12 at 09:57
  • @Marius Not at all. You can build on top of Stefan's code. That's what I mean. Start from Stefan's code and what the little extra that you need. – David Heffernan Mar 21 '12 at 10:02
  • @DavidHeffernan Just joking with you, i'll have a look at reusing his code and amending any missing functionalities. – Marius Mar 21 '12 at 10:14
  • 2
    @Marius Feel free to contribute to the [original source]( http://code.google.com/p/delphisorcery/source/browse/trunk/Source/Core/DSharp.Core.Reflection.pas). – Stefan Glienke Mar 21 '12 at 12:50