TLama

Untitled

Oct 22nd, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.54 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections, Card;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     procedure FormCreate(Sender: TObject);
  12.     procedure FormDestroy(Sender: TObject);
  13.   private
  14.     FCards: TObjectList<TCard>;
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.dfm}
  25.  
  26. procedure TForm1.FormCreate(Sender: TObject);
  27. var
  28.   I: Integer;
  29.   Card: TCard;
  30. begin
  31.   // create a collection instance
  32.   FCards := TObjectList<TCard>.Create;
  33.   // loop to make 20 cards
  34.   for I := 1 to 20 do
  35.   begin
  36.     // create a TCard instance and set whatever you need; this variable will be
  37.     // overwritten, but in this case without a leak, because we're passing it to
  38.     // the collection, which will keep the reference to the created instance and
  39.     // can even free it for us
  40.     Card := TCard.Create(nil);
  41.     Card.Parent := Self;
  42.     Card.Left := I * 50;
  43.     Card.Top := 8;
  44.     // do whatever else with a Card instance and then add it to the collection,
  45.     // but DO NOT FREE IT! now the collection itself is responsible for that and
  46.     // if you want to ever access this instance, you can't release it now
  47.     FCards.Add(Card);
  48.   end;
  49. end;
  50.  
  51. procedure TForm1.FormDestroy(Sender: TObject);
  52. begin
  53.   // and if you free the collection which owns the objects, it will release all
  54.   // the TCard instances we've made before
  55.   FCards.Free;
  56. end;
  57.  
  58. end.
Advertisement
Add Comment
Please, Sign In to add comment