Using TObjectList

Making life easier with TObjectList

New to Delphi 5 & 6 is the TObjectList. As it's name implies it's capable of storing a list of objects!



Recently I was required to submit a sample application for a job I was pursuing. One thing this application did was allowed the user to drop "Comment boxes" on an image. These requirements made the TObjectList class a perfect candidate!



Consider this modified procedure:



procedure TMainForm.AddString;

var

  MyStringList: TStringList;

begin

  MyStringList := TStringList.Create;

  try

    MyStringList.Add('blah');

    MyStringList.Add('blah2');

  except

    FreeAndNil(MyStringList);

  end;

end;



This procedure obviously introduces a pretty serious memory leak! Using the TObjectList can save us the headache of using a standard TList because of a wonderful property/paramter called "OwnsObjects".



Consider our modified procedure:



Let's begin by creating the TObjectList when the form is created.



procedure TMainForm.FormCreate(Sender: TObject);

begin

  FList := TObjectList.Create(True); // OwnsObjects is True.

end;



procedure TMainForm.AddString;

var

  MyStringList: TStringList;

begin

  MyStringList := TStringList.Create;

  try

    MyStringList.Add('blah');

    MyStringList.Add('blah2');

    FList.Add(MyStringList);

  except

    FreeAndNil(MyStringList);

  end;

end;



Now we do not have to worry about the memory leak because when the main form is destroyed we have the following code:



procedure TMainForm.FormDestroy(Sender: TObject);

begin

  FList.Free;

end;



TObjectList allows you to add an object of any kind to it which means it's required to be generic. Let's take a look at the .Add method of the TObjectList itself:



function TObjectList.Add(AObject: TObject): Integer;

begin

  Result := inherited Add(AObject);

end;



Notice the parameter type is TObject and as every good Delphi programmer should know TObject is the absolute starting point for any class in the Delphi VCL. By haveing a parameter of TObject you can pass anything into the list!!



There is a disadvantage however. Referencing the item in the list gives you only a "TObject". To address an item in your list some type casting is required. Perhaps we need to find the string in the list with text containing: "blah". Obviously TObject doesn't have a property of Text so we'll need to do some typecasting.



procedure FindText;

var

  i: Integer;

begin

  for i := 0 to FList.Count - 1 do

  if TStringList(FList.Items[i])[0] = 'blah' then

    if TStringList(FList.Items[i]).Text = 'blah' then

      ShowMessage(TStringList(FList.Items[i]).Text);

  end;

end;



Adding more then one type of class to the same ObjectList is also a possibility! You can call the TObjectLists method entitled: FindInstanceOf.



Here the wonderful tool called "Class Reference" comes in handy. Pass in the reference to your class type and FindInstanceOf will return the index value of where in the list the item is located:



  FList.FindInstanceOf(TStringList, True, 0);



This concludes the FindObject lesson and I hope you see it's value and I hope this article was of assistance!



Eric "DelphiDev" Brown

 

Share this article!

Follow us!

Find more helpful articles: