Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did Delphi ever get a for each loop?

I've read that Delphi was supposed to get a for each loop in Delphi 9. Did this functionality ever make it into the language? My Delphi 2009 IDE doesn't seem to recognize the for each syntax. Here's my code:

  procedure ProcessDirectory(p_Directory, p_Output : string);
  var
    files : TStringList;
    filePath : string;
  begin
    files := GetSubfiles(p_Directory);
    try
      for (filePath in files.Strings) do
      begin
        // do something
      end;

    finally
      files.Free;
    end;
  end;
like image 748
Ryan Avatar asked Apr 19 '10 16:04

Ryan


2 Answers

procedure ProcessDirectory(p_Directory, p_Output : string); 
var 
  files : TStringList; 
  filePath : string; 
begin 
  files := GetSubfiles(p_Directory); 
  try 
    for filePath in files do 
    begin 
      // do something 
    end; 

  finally 
    files.Free; 
  end; 
end; 
like image 70
da-soft Avatar answered Dec 16 '22 12:12

da-soft


Yes.

But it is for..in

Try

var
  s: string;
  c: char;

begin
  s:=' Delphi Rocks!';
  for c in s do  //<--- here is the interesting part
  begin
    Application.MainForm.Caption:=Application.MainForm.Caption+c;
    Sleep(400); //delay a little to see how it works
  end;
like image 27
John Thomas Avatar answered Dec 16 '22 11:12

John Thomas