Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#40

Re: Sinnvoller Einsatz von goto

  Alt 22. Mär 2010, 17:26
Noch etwas Öl oder Code für's Feuer.
Also ich habe bei dieser Funktion noch keinen Weg gefunden, es ohne Goto elegant zu formulieren.
Delphi-Quellcode:
// Prüft, ob eine Datei für den Import vorhanden ist.
// Falls nicht, kann der Benutzer entscheiden, was passieren soll
function CheckImportFile(const fname: TFilename; const Caption:string): Boolean;
var
   msg: string;
label again;
begin
   again:
   if not FileExists(fname) then
      msg := Format('Datei <%s> ist nicht vorhanden', [fname])
   else if FileGetSize(fname) = 0 then
      msg := Format('Datei <%s> ist leer', [fname])
   else
      msg := '';

   if msg <> 'then
   begin
      case MessageBox(0, PChar(msg), PChar(Caption), MB_ICONWARNING or MB_ABORTRETRYIGNORE or MB_SETFOREGROUND) of
        idAbort: Abort; // stille Exception
        idRetry: goto again;
        idIgnore:
        begin
           Result := False; // Datei vorhanden oder leer; der Benutzer möchte die Datei ignorieren
           Exit;
        end;
      end;
   end;
   Result := True; // Datei vorhanden und nicht leer
end;
Andreas
  Mit Zitat antworten Zitat