Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is the difference between <listview.Clear> and <listview>.items.clear in delphi 7?

Tags:

delphi

I would like to know why are there 2 different ways of clearing out a listview. One is by calling listview.clear and other is listview.items.clear. Actually, this extends to many other VCL components too. which method must be used and why?

like image 636
CyprUS Avatar asked Apr 16 '12 07:04

CyprUS


2 Answers

ListView.Clear is just a wrapper around ListView.Items.Clear with ListItems.BeginUpdate/ListItems.EndUpdate. look at the source:

procedure TCustomListView.Clear;
begin
  FListItems.BeginUpdate;
  try
    FListItems.Clear;
  finally
    FListItems.EndUpdate;
  end;
end;

From the docs:

The BeginUpdate method suspends screen repainting until the EndUpdate method is called. Use BeginUpdate to speed processing and avoid flicker while items are added to or deleted from a collection.

A better practice is to use BeginUpdate/EndUpdate for speed and avoiding flicker.
But the main reason to use ListView.Clear is because using a "high-level VCL methods" (As well commented by @Arnaud) is always a good idea, and the implementation might change (BTW, the method was introduced in D7).


EDIT: I have tested the TListView with 10k Items (D7/WinXP):

  • ListView.Items.Clear: ~5500 ms
  • ListView.Clear: ~330 ms

Conclusion: ListView.Clear is about 16 times faster than ListView.Items.Clear when BeginUpdate/EndUpdate is not used!

like image 140
kobik Avatar answered Oct 08 '22 13:10

kobik


ListView.Clear is a convenience method that calls ListView.Items.Clear internally. There is no semantic difference no matter which of the two you call.

I prefer the first one because it is shorter and it doesn't show the internal representation which is of no interest for me at this point.

like image 44
jpfollenius Avatar answered Oct 08 '22 13:10

jpfollenius