Why does memo.Lines use TStrings instead of TStringList?
Asked Answered
H

1

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?

Hughmanick answered 20/6, 2012 at 14:50 Comment(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?Sympathetic
TStrings is a lesser "contract" allowing greater flexibility for the developer. You can interact with any class that implements the TStrings abstract class.Kyliekylila
C
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;
Chronology answered 20/6, 2012 at 15:6 Comment(4)
Okay thanks, that makes sense. So I guess e.g. Memo.Lines.Delete(0) works because it doesn't internally use TStrings?Hughmanick
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. ;)Chronology
Surely there's a slicker way to sort in-place.Sympathetic
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.Rebane

© 2022 - 2024 — McMap. All rights reserved.