Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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;
    FListItems.Clear;
  finally
    FListItems.EndUpdate;

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!

    Actually, I don't think that BeginUpdate .. EndUpdate is of any help in the case of clear. – jpfollenius Apr 16, 2012 at 7:20 @Smasher Since clear release each object, it will notify each deletion to the VCL, unless the BeginUpdate / EndUpdate is used. This is the difference between the two, and why Clear is much faster than Items.Clear. It is always a good idea to call directly the high-level VCL methods, instead of going into the internal plumbing, unless you know exactly what you are doing. – Arnaud Bouchez Apr 16, 2012 at 9:34 @ArnaudBouchez, +1 for "high-level VCL methods". that was exactly the expression I was looking for. – kobik Apr 16, 2012 at 9:42

    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.

    I expect no performance issue. The one just calls the other with the BeginUpdate .. EndUpdate not making a difference in this case (imho). – jpfollenius Apr 16, 2012 at 7:21 -1 for saying it is a mere convenience method without any other difference. There is clearly a difference. – Rudy Velthuis Apr 16, 2012 at 12:10

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.