Ever wonder how to conditionally implement newer features in your code while keeping the ability to use older versions of Xojo at the same time? It’s really easy to do, I’ll show you how.
There’s an advanced feature in Xojo called Compiler Directives which allows you to direct the compiler to conditionally compile a block of code based on a boolean comparison. For instance, if you wanted to temporarily disable a block of code (as opposed to commenting it out) you could wrap your code like this:
#If False // Code that you don't want to compile #EndIf
You can also use constants with the #if directive, so if you created a module called “Switches” and it contained a Public or Protected constant named “EnableDeepLearning” with a value of True or False you could write code like this:
#If Switches.EnableDeepLearning // Code for your deep learning feature goes here // This code will only be included in your app if EnableNewFeature is set to True #EndIf
There are also some built-in constants for you to use. For instance, there are two constants that tell you which version of the IDE is running, one is a Double, the other is a String. Here’s an example which allows you to use the new Dark Mode capability depending on whether you’re running 2018r3 or something earlier:
#If XojoVersion < 2018.03 Var myTextColor As Color = &c000000 #Else Var myTextColor As Color = &c1C1C1C If IsDarkMode Then myTextColor = &cFEFEFE End If #EndIf
There are also build-in constants per platform and processor type:
#If TargetMacOS // macOS Specific Code #ElseIf TargetWindows // Windows Specific Code #ElseIf TargetLinux // Linux Specific Code #ElseIf TargetIOS // iOS Specific Code #EndIf #If Target32Bit // Code for 32-Bit Processors #ElseIf Target64Bit // Code for 64-Bit Processors #ElseIf TargetARM // Code for ARM Processors #EndIf
For more information on Compiler Directives, see https://documentation.xojo.com/#If…#Endif
In addition to code for specific platforms, sometimes you need to know the specific version of the OS that the user is running. In 2019r3 we added the System.Version method specifically for this purpose:
#If TargetMacOS If System.Version = "10.15" Then // Code that works exclusively on 10.15 ElseIf System.Version >= "10.15.1" // Code that only works on 10.15.1 and above End If #EndIf
If you’re working with Windows, keep in mind that System.Version returns NT System Versions, so you’ll need to compare to these values instead:
- Windows 10 = NT 10
- Windows 8.1 = NT 6.3
- Windows 8 = NT 6.2
- Windows 7 = NT 6.1
- Windows Vista = NT 6.0
For more information about System.Version, See http://documentation.xojo.com/api/os/system.html#system-version.