Delphi-PRAXiS
Seite 5 von 5   « Erste     345   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Sinnvoller Einsatz von goto (https://www.delphipraxis.net/149370-sinnvoller-einsatz-von-goto.html)

Neutral General 22. Mär 2010 17:29

Re: Sinnvoller Einsatz von goto
 
Zitat:

Zitat von shmia
Noch etwas Öl oder Code für's Feuer. :stupid:
Also ich habe bei dieser Funktion noch keinen Weg gefunden, es ohne Goto elegant zu formulieren.

repeat-until? :wink:

Tyrael Y. 22. Mär 2010 17:37

Re: Sinnvoller Einsatz von goto
 
Zitat:

Zitat von himitsu
Zitat:

Zitat von Tyrael Y.
Wenn es um die Lesbarkeit geht ist folgende Variante,

Wenn ich mich nicht irre, sollte der Compiler hier etwas vonwegen "Result könnte undefiniert sein" von sich geben.

Hast Recht himitsu, denk dir einfach ein try finally drumherum :D

Zitat:

Zitat von Tyrael Y.

Delphi-Quellcode:
function DemoThread(const AParentHandle : THandle): Integer;
var
  i: Integer;
begin
  try
    for i := 0 to 9 do
    begin
       ...
    end;

    if not Abort then
    begin
      SendFinished(AParentHandle);
    end;
  finally
    Result := 0;
  end;
end;


BUG 22. Mär 2010 17:41

Re: Sinnvoller Einsatz von goto
 
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;
begin
  Result := false;
  repeat
    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
         idIgnore: Exit;
         // idRetry: // noch ne Runde;
       end;
    end else Result := True; // Datei vorhanden und nicht leer
  until Result = true;
end;
@Roter Kasten fehlt: Oh, war jmd. schneller :|

himitsu 22. Mär 2010 17:43

Re: Sinnvoller Einsatz von goto
 
Delphi-Quellcode:
function CheckImportFile(const fname: TFilename; const Caption:string): Boolean;
var
  msg: string;
begin
  Result := True; // Datei vorhanden und nicht leer
  repeat
    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
      Break;
    case MessageBox(0, PChar(msg), PChar(Caption),
        MB_ICONWARNING or MB_ABORTRETRYIGNORE or MB_SETFOREGROUND) of
      idAbort: Abort; // stille Exception
      idRetry: {Continue};
      idIgnore: Result := False; // Datei vorhanden oder leer; der Benutzer möchte die Datei ignorieren
    end;
  until not Result;
end;
es geht aber auch ein while Result do begin ... end;

@BUG: =true ist böse ... sowas macht man einfach nicht. (Gründe stehen zu Genüge im Forum)

BUG 22. Mär 2010 17:58

Re: Sinnvoller Einsatz von goto
 
Zitat:

Zitat von himitsu
@BUG: =true ist böse ... sowas macht man einfach nicht. (Gründe stehen zu Genüge im Forum)

Aua, ja weiß ich, war wohl etwas geschludert :duck:
Trotzdem Danke für den Hinweis.

EDIT:
Aber müsste das nicht until result; heißen, im Moment bleibst du in der Schleife bis result unwahr ist.

himitsu 22. Mär 2010 19:20

Re: Sinnvoller Einsatz von goto
 
Zitat:

Zitat von BUG
Aber müsste das nicht until result; heißen, im Moment bleibst du in der Schleife bis result unwahr ist.

Nee nee, ist schon OK so ... in unseren beiden Codes wird das Result innerhalb der Schleife andersrum behandelt.


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:15 Uhr.
Seite 5 von 5   « Erste     345   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz