Directive

#If...#Endif


Description

Used to control conditional compilation.

Usage

#If TargetBoolean Then
  ' Target-specific code
#ElseIf TargetBoolean Then
  ' Other Target-specific code
#Else
  ' Other Target-specific code
#EndIf

OR

If TargetBoolean Then Target-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 following constants are used: DebugBuild, XojoVersion, Target32Bit, Target64Bit, TargetAndroid, TargetARM, TargetBigEndian, TargetDesktop, TargetConsole, TargetiOS, TargetLinux, TargetLittleEndian, TargetMacOS, TargetMachO, TargetMobile, TargetMobileDevice, TargetMobileSimulator, TargetRemoteDebugger, TargetWindows, TargetWeb, TargetX86, TargetXojoCloud

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 = "2023r3" 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

Compatibility

All project types on all supported operating systems.