views:

383

answers:

1

I have the following code:

var
  sl: THashedStringList;
begin
  sl:= THashedStringList.Create;
  sl.Duplicates := dupIgnore;
  sl.Add('12345');
  sl.Add('12345');
  sl.Add('12345');
  sl.Add('12345');
  sl.Add('12345');
  sl.Add('12345');
  sl.Add('12345');
  ShowMessage(IntToSTr(sl.Count));
end;

But when I see sl.Count, it gives me 7. What is the bug in this?

+13  A: 

You need to set the Sorted property to TRUE in order to have the list ignore duplicates. The property is inherited from TStringList, and if you look at the documentation for TStringList.Duplicates you will find:

Note: Duplicates does nothing if the list is not sorted.

mghie
And that's because THashedStringList inherits its Duplicates property from TStringList, as well as its behavior for the Add method. There's nothing special about THashedStringList in this Pavan's example.
Rob Kennedy
Thanks for the comment, I edited the answer accordingly.
mghie
Sorry, dumb mistake.
Pavan
@Pavan, no good question.
skamradt