PDA

View Full Version : 'with do' question



laggyluk
28-03-2013, 09:33 AM
Is there a way to do that? :


with TLabel.create(nil) do begin
...
someList.Add(this TLabel?);
end;

other than of course

L:=tlabel.create
with L do begin
...
someList.Add(L)
end

User137
28-03-2013, 01:36 PM
No that's not possible. Best you can do is:

someList.Add(TLabel.create(form1));
with TLabel(someList.Items[someList.Items.Count-1]) do begin
...
end;
(You shouldn't create them with nil. Otherwise you have to do the garbage collection (freeing them) all by yourself.)

laggyluk
29-03-2013, 09:14 AM
I actually don't do that but it was shorter to type this way ;)
thanks

SilverWarior
30-03-2013, 01:31 PM
If you are adding all of this labels to some list you could go and create your own TMyLabel class which is deprecated class of TLabel. Doing so you can change default TLabel constructor so that Label gets added into your list ass soon as it is created:

type
TMyLabel = class(TLabel)
public
constructor Create(Owner: TComponent; List: TObjectList);
end;

....

constructor TMyLabel.Create(Owner: TComponent; List: TList);
begin
inherited Create(Owner);
List.Add(self);
end;

And now you can simply use:

with TMyLabel.create(Form1,someList) do begin
...
end;