Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections, Card;
- type
- TForm1 = class(TForm)
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- FCards: TObjectList<TCard>;
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- var
- I: Integer;
- Card: TCard;
- begin
- // create a collection instance
- FCards := TObjectList<TCard>.Create;
- // loop to make 20 cards
- for I := 1 to 20 do
- begin
- // create a TCard instance and set whatever you need; this variable will be
- // overwritten, but in this case without a leak, because we're passing it to
- // the collection, which will keep the reference to the created instance and
- // can even free it for us
- Card := TCard.Create(nil);
- Card.Parent := Self;
- Card.Left := I * 50;
- Card.Top := 8;
- // do whatever else with a Card instance and then add it to the collection,
- // but DO NOT FREE IT! now the collection itself is responsible for that and
- // if you want to ever access this instance, you can't release it now
- FCards.Add(Card);
- end;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- // and if you free the collection which owns the objects, it will release all
- // the TCard instances we've made before
- FCards.Free;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment