Reputation: 2593
I am trying to check if a component is type TMachine
or TLabel
if so i want it to free it as long as its NOT Label1 or label2
but it will not let me do an OR
on if Components[I] is (TMachine) or (TLabel) then
Any way to fix this?
procedure TfDeptLayout.FormClose(Sender: TObject; var Action: TCloseAction);
var
I: Integer;
begin
for I := ComponentCount -1 downto 0 do
begin
if Components[I] is (TMachine) or (TLabel) then
if Components[I].Name <> Label2.Name then
if Components[I].Name <> label3.Name then
components[I].Free;
end;
end;
Upvotes: 0
Views: 1692
Reputation: 5975
You cannot use or
in that way, you need to "repeat" the is
as well.
You also don't need to use Components[I].Name, it suffices to simpy compare the Components[I] reference with the Label1 and Label2 references:
procedure TfDeptLayout.FormClose(Sender: TObject; var Action: TCloseAction);
var
I: Integer;
begin
for I := ComponentCount -1 downto 0 do
begin
if (Components[I] is TMachine) or (Components[I] is TLabel) then
if Components[I] <> Label2 then
if Components[I] <> label3 then
components[I].Free;
end;
end;
it would also be possible (but maybe less readable) to combine all of the conditionals:
procedure TfDeptLayout.FormClose(Sender: TObject; var Action: TCloseAction);
var
I: Integer;
comp: TComponent;
begin
for I := ComponentCount -1 downto 0 do
begin
Comp := Components[I];
if (Comp is TMachine) then
Comp.Free
else if (Comp is TLabel) and (Comp <> Label2) and (Comp <> Label3) then
Comp.Free;
end;
end;
As an aside, it is best to give components that are referred to in code a meaningful name, rather than using the default Label1, Label2... names
Upvotes: 3