条件代码块

时间:2018-01-02 15:36:18

标签: delphi

任何人都知道使用条件代码块的好解决方案或文档吗?

示例如果代码看起来像......

DoSomethingImportant1,LoadData是具有布尔结果的函数。

var 
 lOk: Boolean;
begin
 lOk := False;

 lOk := DoSomethingImportant1;

 if lOk then
 begin
  LoadData; 
  lOk := SaveData;
 end;


 DoSomethingwithSavedData1...
 DoSomethingwithSavedData2...

 if lOk then
  lOk := DoSomethingImportant2
 else GiveSpecificErrorAndExit;* 

 etc...  

如果DoSomethingXX程序发生100次,我不想保存所有条件。

对不起我的英文

2 个答案:

答案 0 :(得分:1)

你可以这样做:

if not DoSomething then
  raise Exception.Create('Error ...');

DoSomething2;    
DoSomething3;

if not DoSomething4 then
  raise Exception.Create('Error ...');

if not DoSomething5 then
  raise Exception.Create('Error ...');

DoSomething6;
DoSomething7;

答案 1 :(得分:1)

有什么问题
begin
  if not DoSomethingImportant1 then
  begin 
    GiveSpecificErrorAndExit;*  
  end; 

  if LoadData and Save then
  begin
    DoSomethingwithSavedData1...
    DoSomethingwithSavedData2...
  end

  else
  begin
    GiveSpecificErrorAndExit;* 
  end;

  if not DoSomethingImportant2 then
  begin 
    GiveSpecificErrorAndExit;*  
  end; 

 etc...  

如果这不是您要找的答案类型,我会删除。