5

I have the following class:

TTest = class
private
  FId: Integer;
  FSecField: Integer;
  FThirdField: Integer;
public
  constructor Create(AId, ASecField, AThirdField: Integer);
  // ..... 
end;

Then I create a TObjectDictionary like this:

procedure TMainForm.btnTestClick(Sender: TObject);
var
  TestDict: TObjectDictionary<TTest, string>;
  Instance: TTest;
begin
  TestDict := TObjectDictionary<TTest, string>.Create([doOwnsKeys]);

  try
    TestDict.Add(TTest.Create(1, 1, 1), '');

    if TestDict.ContainsKey(TTest.Create(1, 1, 1)) then
      ShowMessage('Match found')
    else
      ShowMessage('Match not found');

    Instance := TTest.Create(1, 1, 1);
    TestDict.Add(Instance, 'str');

    if TestDict.ContainsKey(Instance) then
      ShowMessage('Match found')
    else
      ShowMessage('Match not found');
  finally
    TestDict.Free;
  end;
end;

The result is: "Match not found", "Match found". What should I do in order to compare the values of fields of each key but not their addresses?

bob_saginowski
  • 1,429
  • 2
  • 20
  • 35

1 Answers1

4

The default equality comparer for an instance reference variable compares the reference and not the object. So you get object identity rather than value identity.

So you need to supply a custom equality comparer if you wish to impose value identity.

TestDict := TObjectDictionary<TTest, string>.Create(
  [doOwnsKeys], 
  TEqualityComparer<TTest>.Construct(EqualityComparison, Hasher)
);

where EqualityComparison, Hasher are comparison and hash functions. They could be implemented like this:

EqualityComparison := 
  function(const Left, Right: TTest): Boolean
  begin
    Result := (Left.FId = Right.FId)
      and (Left.FSecField = Right.FSecField)
      and (Left.FThirdField = Right.FThirdField);
  end;

Hasher := 
  function(const Value: TTest): Integer
  begin
    Result := BobJenkinsHash(Value.FId, SizeOf(Value.FId), 0);
    Result := BobJenkinsHash(Value.FSecField, SizeOf(Value.FSecField), Result);
    Result := BobJenkinsHash(Value.FThirdField, SizeOf(Value.FThirdField), Result);
  end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thank you @David. One thing to add: the compiler advises me to use `System.Hash.THashBobJenkins.Hash` instead of `BobJenkinsHash` because `BobJenkinsHash` is deprecated. – bob_saginowski Jan 13 '16 at 10:11
  • That sounds plausible. I'm still on XE7 for day to day so that deprecation isn't on my radar. – David Heffernan Jan 13 '16 at 10:35