21

Why does Memo.Lines use the abstract class TStrings? Why doesn't it use TStringList instead?

And should I convert it to TStringList before working with it?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Illiou
  • 315
  • 1
  • 2
  • 9
  • 2
    `TMemo.Lines is TStringList = False`. In fact the actual object that is behind this is a wrapper around the Windows API. The storage is organised by the Windows EDIT control that implements `TMemo`. "Should I convert it to TStringList before working with it?" No. Why would you want to do that? – David Heffernan Jun 20 '12 at 14:58
  • 5
    TStrings is a lesser "contract" allowing greater flexibility for the developer. You can interact with any class that implements the TStrings abstract class. – Simon Jun 20 '12 at 15:02

1 Answers1

26

TMemo.Lines, TListBox.Items, TComboBox.Items, etc. ; all are declared as TStrings. Beware, talking about the property that is! The internal created types are TMemoStrings, TListBoxStrings and TComboBoxStrings respectively, which are all descendants of TStrings and differ all in way of storage.

And why? For interchangeability and interoperability. So every TStrings-descendant has the same properties, and so you can do:

Memo1.Lines := ListBox1.Items;

How to use? Well, a TStrings property like TMemo.Lines works just fine. You can add, delete, change, renew and clear the strings (and objects) on the property, because internally it is a TMemoStrings which implements all this interaction. Declaration <> implementation.

But when you want any special handling, e.g. like sorting which TStringList provides, then you need help. You cannot typecast nor convert a TMemo.Lines to a TStringList, because it isn't one, but instead you need to create an intermediate object for this special processing:

var
  Temp: TStringList;
begin
  Temp := TStringList.Create;
  try
    Temp.Assign(Memo1.Lines);
    Temp.Sort;
    Memo1.Lines.Assign(Temp);
  finally
    Temp.Free;
  end;
end;
NGLN
  • 43,011
  • 8
  • 105
  • 200
  • 1
    Okay thanks, that makes sense. So I guess e.g. Memo.Lines.Delete(0) works because it doesn't internally use TStrings? – Illiou Jun 20 '12 at 15:41
  • Yes. When dropping a memo component on your form doesn't result in an _abstract compiler error_, let's then assume that there is no abstract object being instantiated. ;) – NGLN Jun 20 '12 at 15:59
  • 1
    Surely there's a slicker way to sort in-place. – David Heffernan Jun 20 '12 at 16:26
  • 5
    Calling `Memo.Lines.Delete()` delegates to the appropriate Win32 API call that actually deletes a line from the UI control. The APIs are different for different controls, that is why they use different TStrings-derived classes. That is the beauty of using an abstraction layer like `TStrings`. The caller doesn't have to care HOW it is implemented internally. – Remy Lebeau Jun 20 '12 at 18:30