Wish Define Conditional Symbols in Code

Widget

Well-Known Member
Licensed User
Longtime User
The conditional variable construct is quite useful:
B4X:
#IF FreeOrAmazon
  ...
#ELSE 
  ...
#END IF

but the actual conditional symbol like "FreeOrAmazon" have to be defined in the Project > Build Configurations.

I'd like to have the ability to define these conditional symbols in code, as in:
B4X:
'This statement defines the conditional symbol "FreeOrAmazon"
#DEF FreeOrAmazon

'This undefines it, if it was previously defined earlier in the code.
#UNDEF FreeOrAmazon

It is much easier to maintain conditional symbols in code because then I can use:

B4X:
#IF FreeOrAmazon
  #UNDEF REPORTS    'Only necessary if REPORTS previously defined earlier in code
  #DEF ADVERTISE
#ELSE 
  #DEF REPORTS
  #DEF CLOUDSTORE
#END IF

Now I can set up program options in a central location. With Delphi it was usually in an include file like
#INCLUDE settings.inc

and this include statement would be at the top of each unit (program file). That's because with Delphi the conditional symbols when defined in code using DEF & UNDEF are local to each unit.

Allowing the developer to define the conditional symbols in code will allow him to easily tailor different versions of his app using the same code set by referencing other conditional symbols like #CLOUDSTORE instead of #FREEORAMAZON because later you may decide to allow #CLOUDSTORE for Amazon.

It gets even better if you are developing for cross platforms. You could for example turn on or off a variety of conditional symbols (conditional options) depending on whether the user is compiling under B4A or B4I or B4R etc.. Now your code can reference #IF OPTIONx instead of #IF IOS etc. This makes the code more readable because the features are defined using conditional symbols in a central location. If you can't get a feature to work in iOS, then you simply turn off OPTIONx for iOS in one location as in:

B4X:
#IF B4I
  #UNDEF OPTIONx
#ELSE
  #DEF OPTIONx
#END IF

This allows your code to reference #IF OPTIONx instead of #IF B4I. If you later get that option to work in iOS, then just change the #UNDEF OPTIONx to #DEF OPTIONx for B4i in that one location instead of hunting down all references for "#IF B4I" that relate to OPTIONx.

Even if you don't implement #INCLUDE <file>, you could have a global <ProjectName>Settings.Inc file that allows the conditional symbols to be defined in code as I demonstrated above. You would still keep the Projects > Build Configuration -> Conditional Symbols because they get defined first, then the file <ProjectName>Settings.Inc defines or undefines the conditional symbols as it sees fit.

TIA
 
Top