本文介绍了为什么memo.Lines使用TStrings而不是TStringList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Memo.Lines使用抽象类TStrings?为什么不使用TStringList代替?

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

在使用它之前我应该​​将其转换为TStringList吗?

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

推荐答案

TMemo.LinesTListBox.ItemsTComboBox.Items等;全部都声明为TStrings.当心,说的是财产!内部创建的类型分别为TMemoStringsTListBoxStringsTComboBoxStrings,它们都是TStrings的后代,并且在存储方式上也有所不同.

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.

为什么?用于互换性和互操作性.因此,每个TStrings后代都有相同的属性,因此您可以执行以下操作:

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

Memo1.Lines := ListBox1.Items;

如何使用?好吧,像TMemo.Lines这样的TStrings属性也可以正常工作.您可以在属性上添加,删除,更改,更新和清除字符串(和对象),因为在内部它是一个TMemoStrings,它实现了所有这些交互.声明<>实现.

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.

但是,如果您需要任何特殊处理,例如例如TStringList提供的排序,那么您需要帮助.您不能将TMemo.Lines转换或转换为TStringList,因为它不是一个,但是您需要为此特殊处理创建一个中间对象:

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;

这篇关于为什么memo.Lines使用TStrings而不是TStringList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 21:26