Q: How can i add an integer to the object portion of a stringlist item, using AddObject?
A:只需将整数值转换为TObject http://docwiki.embarcadero.com/VCL/en/System.TObject
List.AddObject('A string',TObject(1));
Q:How can a retrieve the integer back from a object property of stringlist item?
A:将对象值转换为整数
AValue := Integer(List.Objects[i]);
Q: How do i free all objects and list when done?
A:您不需要释放对象列表,因为你没有分配内存。所以只需要调用Free
程序的TStringList
.
尝试这个示例应用程序
{$APPTYPE CONSOLE}
uses
Classes,
SysUtils;
Var
List : TStringList;
i : Integer;
begin
try
List:=TStringList.Create;
try
//assign the string and some integer values
List.AddObject('A string',TObject(1));
List.AddObject('Another string',TObject(100));
List.AddObject('And another string',TObject(300));
//Get the integer values back
for i:=0 to List.Count - 1 do
Writeln(Integer(List.Objects[i]));
finally
//Free the list
List.free;
end;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Readln;
end.