Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: delete object on a list

  1. #11
    short and simple:
    Code:
    obby_list.Clear;
    this will free all the items and clear the list, assuming the class of obby_list is TObjectList.
    Last edited by Dan; 24-11-2010 at 06:22 PM.

  2. #12
    Quote Originally Posted by DarkBow View Post
    Just to clarify it a bit, that code snippet will not work, but Srki_82's code will. The problem is that both Remove and Delete methods remove the the object from the list and move all objects that were behind it in the list 1 position up in the list, i.e, when you remove the first object on position 0, the object on position 1 moves to position 0, the object on position 2 to position 1, etc.

    Regarding the memory, as long the property OwnsObjects is true Remove will free the object. Not sure if Delete will.
    thanks both of you, i couldnt find any example of how to remove a single object from a Tobjectlist anywhere

  3. #13
    It's the same principle with any array. "For" works different in C than pascal. For example:
    Code:
    // This loop would print all numbers 1 to 10... They become like constants for the loop at beginning
    num:=10;
    for i:=1 to num do begin
      num:=1;
      writeln(inttostr(i));
    end;
    
    // Same in C would just print 1
    num=10;
    for (int i=1; i<=num; i++) {
      num=1;
      printf("%d",i);
    }
    So you have to restrict what you are deleting from the array or list. Simplest was like mentioned from end to start
    Code:
    for i:=0 to list.count-1 do
      if MarkedForDeletion(list[i]) then list.Delete(i);
    But ofc you can do it other ways like:
    Code:
    i:=0;
    while (i<list.count) do begin
      if MarkedForDeletion(list[i]) then list.Delete(i)
      else inc(i);
    end;

  4. #14
    ohhh so the listcount isnt updated every loop in a FOR loop
    but in a WHILE loop it IS updated?

    I take it that is a compiler optimisation thingy

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •