#IF / #ENDIF and self defined constants

I just want to confirm that this is a true statement

  • If in a global module I define a BOOLEAN Constant of “MYTEST” and set that value to FALSE
  • Then I enclose code between #IF MYTEST … #ENDIF , then that code would NOT be compiled into the end product
  • If so, can I define an INTEGER constant? MYTEST = 3 , #IF MYTEST=3 … #ENDIF? or #IF MYTEST>1

I need to create a single code base, that not only will have #IF TARGETxxx but also 3 actual “versions”, where each “version” will need to have some code included, but not other.

I think that’s true. Have you tried?

Dave, I have tried it before with Boolean constants and it works. I have not tried it with an Integer constant, I thought it had to be Boolean, but I could be wrong.

The expression has to be a boolean expression made up of constant values and comparisons IF I recall correctly
So it has to be composed of literals, constants and evaluate to TRUE or FALSE

EDIT : place this in the open event of a windows in a new desktop app and play around with it

Const foo = 1
Const s = "s"

#If (foo = 1) and (s="s") Then
  MsgBox "foo = 1"
#EndIf

and yes this will show the msgbox

[quote=327629:@Norman Palardy]The expression has to be a boolean expression made up of constant values and comparisons IF I recall correctly
So it has to be composed of literals, constants and evaluate to TRUE or FALSE[/quote]

You just beat me to it… But that as not ALWAYS true… I remember a time before expressions were allowed… maybe that was what Merv was remembering

  • karen

Expressions have been allowed since day 1 BUT you can not use properties or other variables, language function calls etc
EVERYTHING has to be basically a constant or literal or something that can be evaluated at compile time which is why no Xojo language functions can be used - the compiler does not RUN the code

Thanks…
I need to refactor a few things in hopes of minimizing the number of times I need to refer to the constants…

What I am trying to do is make an AppleStore version, and a version I can sell outside … The outside version will require registration and other functions that are not needed/allowed in the AppleStore version, but I one only ONE project file, where I can change a constant from TRUE to FALSE and recompile

You can do it with 3 global constants. One would be cleaner if it were possible, but setting one of them to TRUE and the other two to FALSE, is trivial.

isAppleStoreVersion = FALSE
isWebVersion = TRUE
isDemoVersion = FALSE

#If isAppleStoreVersion Then
’ some code
#Elseif isWebVersion Then
’ some code
#Elseif isDemoVersion Then
’ some code
#Endif

I’m pretty sure you are mistaken on that. I remember the use of expressions being a new feature in some version…

To check I pulled out my old RB V3 CD from 2001 to check the docs there… The Entry for #If :

[code]#If#Else#Endif Statement
Used to control conditional compilation.

Syntax
#If TargetBoolean Then
//OS specific code
#Else
//Other OS-specific code
#Endif

Part Type
TargetBoolean Boolean Constant

Description
TargetMacOS, TargetPPC, Target68K, TargetCarbon, TargetWin32, or DebugBuild constant, used to determine the operating system that will include the code the follows.
???
Notes
Use conditional compilation to isolate platform-specific statements such as toolbox calls or AppleEvent routines. The code following the #If statement is included only in the build for that operating system. Target68K, TargetPPC, and TargetCarbon are mutually exclusive subsets of TargetMacOS.
[/code]

Also under RBVersion there is no mention of of using it in conditional compilation

  • karen

Yes it works.
You can see that the contents of the #if actually excludes other code, from the following code

Create a global constant of type integer called testval
set it with a value of 1,2, or 3

In a window’s open event, put this code:

[code] #if testval = 1
dim x as integer
x = 1
msgbox "Constant was 1, x is " + cstr(x)

#endif

#if testval = 2

dim x as boolean
x = true
if x then
  msgbox "Constant was 2 , x is true"
else
  msgbox "Constant was 2 , x is false"
end if

#endif

#if testval = 3

dim x as string
x = "Hello"
msgbox "Constant was 3, x says " + x

#endif[/code]

You can see that I have x defined in 3 completely different ways.
They dont clash because the ‘other two’ definitions dont exist in the compiled app.

Excellent, Jeff! I had no idea that would work. Much cleaner, thanks.

Is there anyway to use this process to include/exclude entire Windows, Methods or Modules?

no
there no place to put things “around” methods , modules, windows etc
you’d need to do

#if booleanValue
Window foo
   
end window
#endif
#if booleanValue
Sub foo()
   
end sub
#endif

and there’s no way to do that

so you use other means to achieve this

methods
use one as the API and inside use #if etc to call the one you really want
however unused methods will not be stripped - unless they are in a module that is unused and the entire module gets stripped

modules
unused modules get stripped - so if you #if etc NEVER call methods in that module it will be stripped

windows are similar in that if they are NOT used they will be stripped

Thanks Norman…
So if for example… Module ABC has 3 methods and each method started and ended with #IF/#ENDIF that resolved to FALSE then the compiled code would have no reference to Module ABC? If so, that is good enough for what I’m seeking :slight_smile:

And if one of the Methods called Window XYZ, then it do would be excluded?

the methods would exist but have no code in them IF there is code in other places that would call those methods

if one of those methods referred to window XYZ and NOTHING else did then the window would also be stripped

when we do this what we have is

  1. a main module that is the overall API the rest of the IDE uses
  2. the module defines a private class interface
  3. a private class in the module - one class each for Windows, Linux, and OS X that implements the interface
  4. the first time a method in the module is called it creates the correct platform class instance & uses that

so on linux the windows & os x code literally doesn’t exist, and on os x the windows & linux code does not exist
but the module does as do the methods

you could organize your code similarly