Jump to content
Jud

Has $ELSEIF stopped working?

Recommended Posts

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

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 by Guest

Share this post


Link to post
Guest
{$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 by Guest

Share this post


Link to post
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 by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×