What's new

#define to Delphi

GuestX

New member
hmm.....
i saw some docs how to port the c++ #define to
Delphi: {$DEFINE} <------- For Delphi
But i can't Work because { + } are for comments...
 

Cyberman

Moderator
Moderator
Ummm real easy..
first {$<directive>} is an Object pascal extension {$ must have no space between for the extension to be noticed by the compilor.

Second
{$DEFINE <name> } allows you define symbol <name> the application though is mostly for conditional compilation for example.

{$DEFINE NEW_WAY}

{$IFDEF NEW_WAY}
inc(A,1);
{$ELSE}
A := A +1;
{$ENDIF}

See? very simple.. if you underfine NEW_WAY then it won't compile inc(A, 1); and using A := A+1; instead.. see?


Cyb
 
OP
G

GuestX

New member
thnx, wanna try...
do you have delphi?

if you have delphi: can you post a little prog
wich demonstrates the use of #define?
 
Last edited:
OP
G

GuestX

New member
i experimented a bit and now i can use DEFINED symbols
in a procedure: :phone:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
{$DEFINE NEW_WAY}
{$IFDEF NEW_WAY}
{$ELSE}
NEW_WAY;
{$ENDIF}


end;
end.

it works fine, no errors :)
 
Last edited:

Top