Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 339
Marius Avatar asked Mar 21 '12 08:03

Marius


1 Answers

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.

like image 133
LU RD Avatar answered Nov 17 '22 15:11

LU RD