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 also see items[i] call.
I am confused with TStringList operations and TList operations.
It adds a pair of items: an entry in the TStringList.Strings
list, and a matching TObject
in the TStringList.Objects
list.
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.
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;
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.
The AddObject
method lets you store a TObject address (pointer) associated to the string stored in the Item property. the Objects
property is for access the stored objects.
Check this simple samplem that uses the AddObject
to store an integer value associated to each string.
var
List : TStringList;
I : integer;
begin
try
List:=TStringList.Create;
try
List.AddObject('Item 1', TObject(332));
List.AddObject('Item 2', TObject(345));
List.AddObject('Item 3', TObject(644));
List.AddObject('Item 4', TObject(894));
for I := 0 to List.Count-1 do
Writeln(Format('The item %d contains the string "%s" and the integer value %d',[I, List[I], Integer(List.Objects[i])]));
finally
List.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
TStringList is more than a list of strings.
It can be used for name value pairs:
stringlist.Values['apple'] := 'one';
stringlist.Values['banana'] := 'two';
But it can also be used to associate strings with any object (or any pointer).
stringlist.AddObject('apple', TFruit.Create);
stringlist.AddObject('banana', TFruit.Create);
i := stringlist.IndexOf('apple');
if i >= 0 then
myfruit := stringlist.Objects[i] as TFruit;
TList is a list that stores pointers. They are not associated with strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With