48

Is it possible to define more than one conditional in one {$IFDEF} directive ?
I would like to have syntax like this:

{$IFDEF Condition1 OR Condition2} DoSomething; {$ENDIF}
{$IFDEF Condition1 AND Condition2} DoSomethingElse; {$ENDIF}

Thanks

Martin Reiner
  • 2,167
  • 2
  • 20
  • 34

3 Answers3

68

You would need to use $IF instead:

{$IF Defined(Condition1) or Defined(Condition2)}
DoSomething;
{$IFEND}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
20

In case you have to support old Delphis (without the support for the $IF metadirective), you can use one simple and one ugly workaround:

//AND
{$IFDEF Cond1}{$IFDEF Cond2}DoSomething{$ENDIF}{$ENDIF} 

//OR
{$UNDEF Cond1OrCond2}
{$IFDEF Cond1}{$DEFINE Cond1OrCond2}{$ENDIF}
{$IFDEF Cond2}{$DEFINE Cond1OrCond2}{$ENDIF}
{$IFDEF Cond1OrCond2}DoSomething{$ENDIF}

If you are repeating the test more than once, first case should be rewritten as follows.

{$UNDEF Cond1AndCond2}
{$IFDEF Cond1}{$IFDEF Cond2}{$DEFINE Cond1AndCond2{$ENDIF}{$ENDIF} 

{$IFDEF Cond1AndCond2}DoSomething{$ENDIF}
gabr
  • 26,580
  • 9
  • 75
  • 141
6

hey try this from the embarcadero.com

begin
  ...
 {$IF Defined(MY_DEFINE) and (LibVersion > 2.0) }
  Writeln(1);
 {$ELSE}
  Writeln(2);  

  ... 
  {$IFEND}
 end;
PresleyDias
  • 3,657
  • 6
  • 36
  • 62