Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TStringList's addObject method

Tags:

delphi

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.

like image 661
Kumar S Avatar asked Jan 20 '12 20:01

Kumar S


3 Answers

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.

like image 113
Ken White Avatar answered Oct 20 '22 16:10

Ken White


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.
like image 5
RRUZ Avatar answered Oct 20 '22 16:10

RRUZ


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.

like image 2
Toon Krijthe Avatar answered Oct 20 '22 18:10

Toon Krijthe