-3

I use this code for emptying fill in the form :

var
i: integer;

for i:=0 to componentcounts-1 do
    begin
    if component[i] is TEdit then
       (component[i] as Tedit).text:='';
   .....another component also include
    end;

but i prefer use this code outside the form, so that can be use by another form

then i create a procedure

procedure emptyForm(f:Tform)
var
   i:integer;
begin
with f do
     begin
     for i:=0 to componentcounts-1 do
         begin
         if component[i] is TEdit then
           (component[i] as Tedit).text:='';
           //.....another component also include
        end;
     end;
end;

its save do this way ?

Hasbi
  • 11
  • 3
  • 1
    Did you mean `Safe`? also, `componentcounts`? - please edit your question and use real code. – kobik Nov 15 '16 at 13:11
  • 1
    It's safe enough in that a program that doesn't compile can't ever run. It might be more interesting to see the real program. – David Heffernan Nov 15 '16 at 13:18
  • sorry @kobik, thanks David Heffernan – Hasbi Nov 15 '16 at 13:33
  • 1
    Safe or not, I would add a method, say, `procedure ClearEdits;` in the form where the `TEdit`s are, and call that method from outside. Outer world is not supposed to know what widets a form has. – Tom Brunberg Nov 15 '16 at 13:58

1 Answers1

-1

It's OK I suppose, but using 'with' is a little dangerous. To see why, remember that TForm is descended from TComponent and has many of the same properties as Component[i] leading to potential confusion and errors. I prefer the following

procedure emptyForm(f:Tform);
var
   i:integer;
   iComponent : TComponent;
begin
     for i:=0 to f.componentcount-1 do
     begin
         iComponent := f.Components[ I ];
         if iComponent is TEdit then
         begin
           (iComponent as Tedit).text:='';
         end;
           //.....another component also include
     end;
end;
Dsm
  • 5,870
  • 20
  • 24