#If...#Endif

From Xojo Documentation

Directive

Used to control conditional compilation.

Usage

#If TargetBoolean [Then]
[#Else]
//Other OS-specific code
[#ElseIf TargetBoolean ]
//Other OS-specific code for this target platform
#Endif

OR

#If TargetBoolean Then OS-specific code

Part Type Description
TargetBoolean Boolean constant or Boolean constant expression. Constant or expression that evaluates to a boolean constant. Used to determine the operating system that will include the code that follows. The DebugBuild, XojoVersion, TargetBigEndian, TargetDesktop, TargetConsole, TargetLinux, TargetLittleEndian, TargetMacOS, TargetMachO, TargetWindows, TargetX86, TargetWeb, Target32Bit, Target64Bit, TargetXojoCloud constants are used.

Notes

#If statements can be written on one line if there are no #Else or #Elseif clauses. In this case, the Then keyword is required and the #endif is not part of the syntax.

You can use conditional compilation to isolate platform-specific statements such as API calls, AppleEvent routines, or console application routines that are specific to Windows, macOS, or Linux. The code following the #If statement is included only in the build for that operating system.

The optional #ElseIf clause enables you to use a template such as this for handling all cases:

#If TargetWindows Then
// Windows-specific code here
#ElseIf TargetMacOS
// Mac-specific code goes here
#ElseIf TargetLinux Then
// Linux-specific code goes here
#ElseIf TargetXojoCloud Then
// Xojo Cloud-specific code goes here
#EndIf

The TargetBoolean parameter must be either a Boolean constant or a Boolean constant expression that evaluates to True or False. For example, you can use XojoVersion or XojoVersionString in a boolean expression to check the version currently being used.

#If XojoVersionString = "2018r1" Then
// Code using features new to this version
#Endif

Sample Code

The following example assigns the file path separator character to a string based on the target platform:

Var separator As String
#If TargetWindows Then
separator = "\"
#ElseIf TargetMacOS Then
separator = "/"
#ElseIf TargetLinux Then
separator = "/"
#Endif

See Also

Declare statement, AppleEvent class; DebugBuild, XojoVersion, XojoVersionString, TargetBigEndian, TargetLittleEndian, TargetDesktop, TargetConsole, TargetLinux, TargetMachO, TargetMacOS, TargetWindows, TargetX86, TargetWeb, Target32Bit, Target64Bit, TargetXojoCloud constants.