Delphi XE: Where is my TValue.Equals()?
Asked Answered
T

1

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.

Thimbleful answered 21/3, 2012 at 8:27 Comment(1)
I think DeHL (now discontinued) has some some stuff to do this. Don't know specifics. code.google.com/p/delphilhlplibPruchno
D
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.

Durst answered 21/3, 2012 at 8:47 Comment(6)
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.Thimbleful
@Thimbleful You can simply add the missing functionality yourself.Meraz
@DavidHeffernan True, though that would render my initial question obsolete :pThimbleful
@Thimbleful 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.Meraz
@DavidHeffernan Just joking with you, i'll have a look at reusing his code and amending any missing functionalities.Thimbleful
@Thimbleful Feel free to contribute to the [original source]( code.google.com/p/delphisorcery/source/browse/trunk/Source/Core/…).Jumble

© 2022 - 2024 — McMap. All rights reserved.