Jud 1 Posted December 21, 2020 In Delphi 10.4.1, the following does NOT set Step to 2. Am I missing something? {$DEFINE test2 } ... Step := 0; {$IFDEF test1} Step := 1; {$ELSEIF test2} Step := 2; {$ENDIF} Share this post Link to post
Guest Posted December 21, 2020 (edited) http://docwiki.embarcadero.com/RADStudio/Sydney/en/Conditional_compilation_(Delphi) http://docwiki.embarcadero.com/RADStudio/Sydney/en/IF_directive_(Delphi) ... {$DEFINE MY_DEFINE} const LibVersion = 2.1; ... begin ... {$IF Defined(MY_DEFINE) and (LibVersion > 2.0) } Writeln(1); {$ELSE} Writeln(2); // this code does not execute {$IFEND} {$IF Defined(MY_DEFINE) } Writeln(3); // this code executes {$ELSEIF LibVersion > 2.0} Writeln(4); // this code does not execute {$ELSEIF LibVersion = 2.0} Writeln(5); // this code does not execute {$ELSE} Writeln; // this code does not execute {$IFEND} // {$IF Declared(Test)} Writeln('Success'); // successful {$IFEND} ... The $IF and $ELSEIF directives are terminated with $IFEND, unlike other conditional directives that use the $ENDIF terminator. This allows you to hide $IF blocks from earlier versions of the compiler (which do not support $IF or $ELSEIF) by nesting them within old-style $IFDEF blocks. For example, the following construction would not cause a compilation error! {$UNDEF NewEdition} {$IFDEF NewEdition} {$IF LibVersion > 2.0} ... {$IFEND} {$ENDIF} If your code needs to be portable between various versions of Delphi or platforms (such as .NET), you will need to test whether or not this directive is supported by the compiler. You can surround your code with the following directives: {$IFDEF conditionalexpressions} . // code including IF directive . // only executes if supported {$ENDIF} hug Edited December 21, 2020 by Guest Share this post Link to post
Guest Posted December 21, 2020 (edited) {$DEFINE test2} procedure TForm1.FormCreate(Sender: TObject); var Step: integer; begin {$IF Defined(test1)} ShowMessage('test1'); {$ELSEIF Defined(test2)} ShowMessage('test2'); // will be showed {$ELSE} ShowMessage('test X'); {$IFEND} // // OR // {$IFDEF test1} ShowMessage('test1'); {$ELSEIF Defined(test2)} ShowMessage('test2'); // will be showed {$ELSE} ShowMessage('test X'); {$ENDIF} end; {$ELSEIF test2} is NOT a CONDITIONAL or be: ELSEIF A=B hug Edited December 21, 2020 by Guest Share this post Link to post
Remy Lebeau 1398 Posted December 22, 2020 (edited) On 12/20/2020 at 8:53 PM, Jud said: In Delphi 10.4.1, the following does NOT set Step to 2. Am I missing something? {$DEFINE test2 } ... Step := 0; {$IFDEF test1} Step := 1; {$ELSEIF test2} Step := 2; {$ENDIF} You need to use {$ELSE}{$IFDEF ...}: {$DEFINE test2} ... Step := 0; {$IFDEF test1} Step := 1; {$ELSE} {$IFDEF test2} Step := 2; {$ENDIF} {$ENDIF} or {$ELSEIF Defined(...)}: {$DEFINE test2} ... Step := 0; {$IFDEF test1} // or: {$IF Defined(test1)} Step := 1; {$ELSEIF Defined(test2)} Step := 2; {$IFEND} // or: {$ENDIF} since XE4 Edited December 22, 2020 by Remy Lebeau Share this post Link to post
Remy Lebeau 1398 Posted December 22, 2020 On 12/20/2020 at 9:00 PM, emailx45 said: The $IF and $ELSEIF directives are terminated with $IFEND, unlike other conditional directives that use the $ENDIF terminator. The {$IF} and {$ELSEIF} directives have supported {$ENDIF} since Delphi XE4. See http://docwiki.embarcadero.com/RADStudio/en/Legacy_IFEND_(Delphi) Share this post Link to post