TStringList的addObject方法 [英] TStringList's addObject method
问题描述
我想知道这个方法调用的方式:
I want to know what this method call does:
stringList.addObject(String,Object);
我也想知道这个属性的作用:
I also want to know what this property does:
stringList.Objects[i]
像键,值对,而添加。但是在循环中检索到哪些内容可以被检索?
It looks like key,value pair while adding. But while retrieving in a loop what gets retrieved?
我也看到项目[i]呼叫。
I also see items[i] call.
对TStringList操作和TList操作感到困惑。
I am confused with TStringList operations and TList operations.
推荐答案
它添加了一对项目: TStringList.Strings
列表,以及 TStringList.Objects
列表中匹配的 TObject
。
It adds a pair of items: an entry in the TStringList.Strings
list, and a matching TObject
in the TStringList.Objects
list.
这允许您例如存储提供项目名称的字符串列表,以及作为包含匹配项目的类的对象。 p>
This allows you to, for instance, store a list of strings that supply a name for the item, and an object that is the class containing the matching item.
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[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;
这显然是一个有创意的例子,因为这不是真的有用的东西。它演示了这个概念。
This is clearly a contrived example, as it's not something you'd really find useful. It demonstrates the concept, though.
另一个方便的用途是存储一个整数值和一个字符串(例如,显示一个 TComboBox
或 TListBox
以及用于数据库查询的相应ID。在这种情况下,您只需要在 Objects
数组中键入整数(或任何其他SizeOf(Pointer))。
Another handy use is for storing an integer value along with a string (for instance, showing a list of items in a TComboBox
or TListBox
and a corresponding ID for use in a database query). In this case, you just have to typecast the integer (or anything else that is SizeOf(Pointer)) in the Objects
array.
// 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 $ c之外的东西的情况下$ c>,你不需要释放内容。由于它们不是真正的对象,除了
TStringList
本身之外,没有什么可以免费的。
In the case of storing things other than an actual TObject
, you don't need to free the contents. Since they're not real objects, there's nothing to free except the TStringList
itself.
这篇关于TStringList的addObject方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!