Jump to content
alogrep

$DEFINE: how does it work?

Recommended Posts

HI

I hope someone can shed some light. I can mke it to work

I have this code (pseudo)

FUNCTION DOTHERPORT;
BEGIN
{$ifDEF  BO}
          result :=DoCharges(true,-1,false,False,false,lg('CLOSE CHARGES'),'1',dm2.sysdate.fieldbyname('date').asdatetime)
{$else}

         result := DoCharges(sender=Nil,-1,False,False,FALSE,lg(TDsFancyButton(Sender).Caption),'',0,dm2.sysdate.fieldbyname('date').asdatetime);
{$endif}
END;

function selectreport(sender: Tobject);
begin
  if sender = good1 then
{$DEFINE BO}
   dotherport;
   
end;

FUNCTION DOTHERPORT;
BEGIN
{$ifDEF  BO}
          result :=DoCharges(true,-1,false,False,false,lg('CLOSE CHARGES'),'1',dm2.sysdate.fieldbyname('date').asdatetime)
{$else}

         result := DoCharges(sender=Nil,-1,False,False,FALSE,lg(TDsFancyButton(Sender).Caption),'',0,dm2.sysdate.fieldbyname('date').asdatetime);
{$endif}
END;

function selectreport(sender: Tobject);
begin
  if sender = good1 then
{$DEFINE BO}
  else
{$define OLD}
   dotherport;
   
end;

I would expect that the line under {$ifdef BO

  would execute. 

Instead it goes to the other line.

How does $DEFINE work?

Thanks.

Share this post


Link to post

Pascal code is compiled top-down in a single pass. DEFINE, IFDEF, etc are compiler directives that are evaluated only at compile-time. The resulting code is fixed, it cannot be changed at runtime. If you try to IFDEF something before it has been DEFINE'd, the result will be False. Hence the name IF-DEFined.

 

In any case, DEFINE/IFDEF is not the correct solution for your problem anyway. You are making a decision based on data known only at runtime, so you need to add an extra parameter to your function that is called at runtime (you need to add a Sender parameter, too), eg:

function doTheReport(Sender: TObject; Bo: Boolean);
begin
  if Bo then
    Result := DoCharges(true, -1, False, False, False, lg('CLOSE CHARGES'), '1', dm2.SysDate.FieldByName('date').AsDateTime)
  else
    Result := DoCharges(Sender=nil, -1, False, False, False, lg(TDsFancyButton(Sender).Caption), '', 0, dm2.SysDate.FieldByName('date').AsDateTime);
end;

function selectReport(Sender: TObject);
begin
  doTheReport(Sender, Sender = good1);
end;

Alternatively:

function doTheReport(Sender: TObject);
begin
  if Sender = good1 then
    Result := DoCharges(true, -1, False, False, False, lg('CLOSE CHARGES'), '1', dm2.SysDate.FieldByName('date').AsDateTime)
  else
    Result := DoCharges(Sender=nil, -1, False, False, False, lg(TDsFancyButton(Sender).Caption), '', 0, dm2.SysDate.FieldByName('date').AsDateTime);
end;

function selectReport(Sender: TObject);
begin
  doTheReport(Sender);
end;

 

Edited by Remy Lebeau
  • Thanks 1

Share this post


Link to post

As explained above, define work top-down as compiletime, pretty much as the "defines" in C/C++.

This is very useful since you can switch between certain code behaviour parts, include or not to compile, depending on a compiletime defined condition.
 

//Can be used with DEFINED() in a more speaking, checkbox-like version
{$DEFINE ___USE_THE_BO_VARIANT }
{$DEFINE _X_USE_THE_MO_VARIANT }
{$DEFINE ___USE_THE_ZO_VARIANT }

FUNCTION DOTHERPORT;
BEGIN
{$ifDEF  _X_USE_THE_BO_VARIANT}
          result :=DoCharges(true,-1,false,False,false,lg('CLOSE CHARGES'),'1',dm2.sysdate.fieldbyname('date').asdatetime)
{$else}

         result := DoCharges(sender=Nil,-1,False,False,FALSE,lg(TDsFancyButton(Sender).Caption),'',0,dm2.sysdate.fieldbyname('date').asdatetime);
{$endif}
END;

//Or maybe in mode complex scenarios, as switch/case-like option, like this
FUNCTION DOTHERPORT2;
BEGIN

{$if     DEFINED( _X_USE_THE_BO_VARIANT ) }
          result :=DoCharges(true,-1,false,False,false,lg('CLOSE CHARGES'),'1',dm2.sysdate.fieldbyname('date').asdatetime)
{$elseif DEFINED( _X_USE_THE_MO_VARIANT ) }
         result := DoCharges(sender=Nil,-1,False,False,FALSE,lg(TDsFancyButton(Sender).Caption),'',0,dm2.sysdate.fieldbyname('date').asdatetime);
{$elseif DEFINED( _X_USE_THE_ZO_VARIANT ) }
          result :=DoCharges(true,-1,false,False,false,lg('ZOOOO'),'1',dm2.sysdate.fieldbyname('date').asdatetime)
{$else                                    }
          result :=DoCharges(true,-1,false,False,false,lg('NOTHING ELSE MATTERS'),'1',dm2.sysdate.fieldbyname('date').asdatetime)
{$endif}

END;

 

Edited by Rollo62

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

×