Results 1 to 10 of 14

Thread: delete object on a list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by slenkar View Post
    AAARRRRGGGHHHHH

    Ive been scouring the internet for an example of how to remove an object from a Tobjectlist with no luck

    Code:
    for iter3:=0 to obby_list.count-1 do
    begin
    obj:=obby_list.items[iter3];
    obby_list.remove(obj);
    end;
    this example crashes with exception 'unknown'
    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.

  2. #2
    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.

  3. #3
    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

  4. #4
    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;

  5. #5
    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

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
  •