- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道这个方法调用的作用:
stringList.addObject(String,Object);
我也想知道这个属性是做什么的:
stringList.Objects[i]
添加时看起来像键、值对。但是在循环检索时检索到了什么?
我还看到 items[i] 调用。
我对 TStringList 操作和 TList 操作感到困惑。
最佳答案
它添加了一对项目:TStringList.Strings
列表中的一个条目,以及 TStringList.Objects
列表中匹配的 TObject
.
例如,这允许您存储提供项目名称的字符串列表,以及包含匹配项目的类的对象。
type
TPerson=class
FFirstName, FLastName: string;
FDOB: TDateTime;
FID: Integer;
private
function GetDOBAsString: string;
function GetFullName: string;
published
property FirstName: string read FFirstName write FFirstName;
property LastName: string read FLastName write FLastName;
property DOB: TDateTime read FDOB write FDOB;
property DOBString: string read GetDOBAsString;
property FullName: string read GetFullName;
property ID: Integer read FID write FID;
end;
implementation
{TPerson}
function TPerson.GetDOBAsString: string;
begin
Result := 'Unknown';
if FDOB <> 0 then
Result := DateToStr(FDOB);
end;
function TPerson.GetFullName: string;
begin
Result := FFirstName + ' ' + FLastName; // Or FLastName + ', ' + FFirstName
end;
var
PersonList: TStringList;
Person: TPerson;
i: Integer;
begin
PersonList := TStringList.Create;
try
for i := 0 to 9 do
begin
Person := TPerson.Create;
Person.FirstName := 'John';
Person.LastName := Format('Smith-%d', [i]); // Obviously, 'Smith-1' isn't a common last name.
Person.DOB := Date() - RandRange(1500, 3000); // Make up a date of birth
Person.ID := i;
PersonList.AddObject(Person.LastName, Person);
end;
// Find 'Smith-06'
i := PersonList.IndexOf('Smith-06');
if i > -1 then
begin
Person := TPerson(PersonList.Objects[i]);
ShowMessage(Format('Full Name: %s, ID: %d, DOB: %s',
[Person.FullName, Person.ID, Person.DOBString]));
end;
finally
for i := 0 to PersonList.Count - 1 do
PersonList.Objects[i].Free;
PersonList.Free;
end;
这显然是一个人为的示例,因为它并不是您真正有用的东西。不过,它演示了这个概念。
另一个方便的用途是存储整数值和字符串(例如,显示 TComboBox
或 TListBox
中的项目列表以及相应的使用 ID在数据库查询中)。在这种情况下,您只需在 Objects
数组中对整数(或任何其他 SizeOf(Pointer))进行类型转换。
// Assuming LBox is a TListBox on a form:
while not QryItems.Eof do
begin
LBox.Items.AddObject(QryItem.Fields[0].AsString, TObject(QryItem.Fields[1[.AsInteger));
QryItems.Next;
end;
// User makes selection from LBox
i := LBox.ItemIndex;
if i > -1 then
begin
ID := Integer(LBox.Items.Objects[i]);
QryDetails.ParamByName('ItemID').AsInteger := ID;
// Open query and get info.
end;
如果存储的内容不是实际的 TObject
,则无需释放内容。由于它们不是真正的对象,因此除了 TStringList
本身之外,没有任何东西可以释放。
关于delphi - TStringList 的 addObject 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947400/
我希望创建类似“TOwnedStringList”(类名是虚构的)的东西,我可以将其构造为: sl := TOwnedStringList.Create(Self); sl.Sorted := Tru
我的环境: C++ Builder XE4 我正在尝试使用 TStringList 的数组使用 unique_ptr<> . 以下没有给出任何错误: unique_ptr vals(new int [
是的,我想将一个简单的日志文件读取到 TStringList 中,使用 LoadFromFile 可以轻松完成。但问题是该文件可能已经被另一个程序同时打开,因此可能会出现异常。我尝试使用: FileM
我有一个用于在 TStringList 中存储许多字符串的应用程序。这些字符串在很大程度上彼此相似,并且我想到可以即时压缩它们 - 即以唯一文本片段加上对先前存储的片段的引用的混合形式存储给定的字符串
可以在没有任何形式同步的情况下从 TStringList 读取数据吗?例如与主线程同步。 示例代码 var MyStringList:TStringList; //declared globally
Delphi 1 16 位(是的,它很旧,但运行良好) 一些示例代码: procedure TForm1.Button1Click(Sender: TObject); var SL: TStrin
Delphi 2007中有没有集成的解决方案来检查TStringList是否包含某个值的一部分? 例如: List.AddObject('This is a string', customString
我正在向 TStringList 添加一些 TObject 后代,例如通过调用 AddObject。我希望当我释放列表对象时它们也被释放。有什么办法可以实现这一点吗? 最佳答案 OwnsObjects
最近,一位信誉良好的 SO 用户通知我,TStringList 存在拆分错误,这会导致它无法解析 CSV 数据。我还没有被告知这些错误的性质,并且在互联网上进行了搜索,包括 Quality Centr
我用 tstringlist 制作了一个非常简单的日志。我将其写入文件: pLog.SaveToFile(FileName); 某处出现错误,我的计算机已关闭。之后,我找不到我的日志文件。可能该文件以
我正在开发一个键盘 Hook 并使用 SetWindowsHookEx,并将每个击键添加到 TStringList 中。为了操纵 TStringList 内的击键,我总是使用“.Text”属性。例如,
我有两个正在使用的字符串列表。一个具有关键字列表,另一个具有否定关键字列表。我希望能够搜索列表并挑选出不包含否定关键字的列表项并输出到第三个关键字列表。我使用的是 AnsiPos 函数,但是如果否定关
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
因此,我有几个要读取并从中获取值的摘要文件。 我正在执行以下操作: OutputSummary := TStringList.Create; for idx := 0 to 82 do Outpu
我有一个自定义排序 TStringList ... Items.CustomSort(@CompareWords); ...使用此比较功能: function CompareWords(List: T
procedure FreeListObjects( l : TStrings); var i : integer; BEGIN FOR i := 0 TO l.Count -1 DO BEG
我正在使用 Delphi 7,并且有一个例程,该例程获取包含一系列记录的 csv 文件并导入它们。这是通过使用 MyStringList.LoadFromFile(csvfile) 将其加载到 TSt
我花了好几天时间寻找如何做到这一点,但没有什么是我真正需要做的(或者我只是不明白如何实现该解决方案)。 我需要做的是将一个字符串(街道地址)解析为 TStringList,然后将列表中的这些字符串设置
我试图在 TStringList 的对象属性内存储一个集合(并读取它)(我还将使用它来存储与该集合关联的文本),但我得到了该集合的无效类型转换。 在 StringList 对象中存储集合的最佳方法是什
我正在编写自己的类来管理 android/ios 应用程序的翻译,并且我已经生成了此代码。我将解释下面的代码,但它非常简单且简短。 unit Localization; interface uses
我是一名优秀的程序员,十分优秀!