Search code examples
delphiequalityrttitvalue

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.


Solution

  • 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.