Recent

Author Topic: TStringList undocumented features  (Read 1020 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2796
    • tomboy-ng, a rewrite of the classic Tomboy
TStringList undocumented features
« on: April 10, 2020, 01:34:13 pm »
I am a little puzzled about two things I have found  about TStringList, it would be nice if someone can confirm this is expected behavior.

1. If you sort the list, the Sorted property is not set to true.  HelpNote is a TStringList and EBox is/are Instances of a Form.  Thus -

Code: Pascal  [Select][+][-]
  1.         HelpList.AddObject(HelpNoteName, EBox);
  2.         HelpList.Sort;
  3.         showmessage('List is sorted ' + booltostr(HelpList.Sorted, true));    // this says False
  4.         HelpList.Find('Some Name', SomeInt);         // This generates an exception

       

The messagebox says 'False' and Find raises an exception because the list is not sorted.  If you set the property to True, it all behaves as expected. In fact, after the call to .Sort it is is sorted, should Sorted set the Sorted property ?

Secondly, and I find this very convenient but want to know if I can depend on it -

Code: Pascal  [Select][+][-]
  1.         HelpList.AddObject(NoteNoteName, EBox1);
  2.         HelpList.AddObject(NoteNoteName, EBox2);
  3.         HelpList.AddObject(NoteNoteName, EBox3);

After this little run, HelpList will contain just one entry (not three) and it's object is EBox3.   Like I said, that suits me perfectly but can I rely on that behavior ?
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: TStringList undocumented features
« Reply #1 on: April 10, 2020, 01:51:29 pm »
Secondly, and I find this very convenient but want to know if I can depend on it -

Code: Pascal  [Select][+][-]
  1.         HelpList.AddObject(NoteNoteName, EBox1);
  2.         HelpList.AddObject(NoteNoteName, EBox2);
  3.         HelpList.AddObject(NoteNoteName, EBox3);

After this little run, HelpList will contain just one entry (not three) and it's object is EBox3.   Like I said, that suits me perfectly but can I rely on that behavior ?

It should depend on the value of the Duplicates property.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: TStringList undocumented features
« Reply #2 on: April 10, 2020, 03:22:47 pm »
In fact, after the call to .Sort it is is sorted, should Sorted set the Sorted property ?

I guess you mean: sould Sort set Sorted to True?

No.
Sorted means that the list needs to be kept sorted when you add/insert items.
For that to work, you first need to sort the list, hence Sorted := True calls Sort().

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: TStringList undocumented features
« Reply #3 on: April 10, 2020, 03:25:56 pm »
should Sorted set the Sorted property
I think not. If you need a list that is always sorted, then set the Sorted property, and if you sometimes sort the list for your own purposes, the property should not be set. In this case, the Add methods work faster.

dbannon

  • Hero Member
  • *****
  • Posts: 2796
    • tomboy-ng, a rewrite of the classic Tomboy
Re: TStringList undocumented features
« Reply #4 on: April 11, 2020, 01:55:34 am »
Ah, thank you folks.

"Duplicates" - I looked for "Unique" sigh..... Duplicates makes perfect sense. Thanks.

Sort - https://lazarus-ccr.sourceforge.io/docs/rtl/classes/tstringlist.sort.html says -
Sort will sort the strings in the list using the quicksort algorithm. If the list has its TStringList.Sorted property set to True then nothing will be done.   That says to me that after I call Sort() the list is sorted and I can call Find().

But re-reading https://lazarus-ccr.sourceforge.io/docs/rtl/classes/tstringlist.sorted.html is quite clear, I don't know how I mis read it yesterday.  Sorted is an instruction to the list to maintain it self in a sorted state, I read it as being an indication to me that the list is or is not currently sorted.

Anyway, sounds like I can do what I need with just the Sorted property, I'll confirm that and make a note in the Wiki.

Thanks folks, great help as usual !

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018