alogrep 0 Posted yesterday at 12:02 AM 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
Darian Miller 386 Posted yesterday at 01:59 AM {$DEFINE BO} is a compiler directive for conditional compilation: https://docwiki.embarcadero.com/RADStudio/en/Conditional_compilation_(Delphi) Once the program is compiled, all {$IFDEF ...} logic is set and does not change at runtime. Share this post Link to post
Remy Lebeau 1618 Posted yesterday at 02:41 AM (edited) 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 yesterday at 02:51 AM by Remy Lebeau 1 Share this post Link to post
Rollo62 589 Posted yesterday at 05:38 AM (edited) 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 yesterday at 05:39 AM by Rollo62 Share this post Link to post