#If…#Endif Statement

Used to control conditional compilation.

# Notes

# Syntax

#If TargetBoolean [Then]

//OS specific code

[#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, RBVersion, TargetBigEndian, TargetCarbon, TargetHasGUI, TargetLinux, TargetLittleEndian, TargetMacOS, TargetMachO, TargetMacOSClassic, or TargetWin32, constants are used.

#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.

Use conditional compilation to isolate platform-specific statements such as API calls, AppleEvent routines, or console application routines that are specific to Windows, Mac OS X, or Linux. The code following the #If statement is included only in the build for that operating system. TargetMacOSClassic, and TargetCarbon are mutually exclusive subsets of TargetMacOS.

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

#If TargetWin32
//Windows specific code here
#ElseIf TargetMacOS
//Macintosh code goes here.
#ElseIf TargetLinux
//Linux code goes right 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 RBVersion or RBVersionString in a boolean expression that determines the user's version of REALbasic.

# Example

The following example assigns values to the (user-defined) Separator property of the App class in its Open event handler. It will be used to specify full pathnames that are correct on Windows, Macintosh and Linux.

#If TargetWin32
Separator=""
#ElseIf TargetMacOS
Separator=":"
#ElseIf TargetLinux
Separator="/"
#Endif

# See Also

Declare statement, AppleEvent class; DebugBuild, RBVersion, RBVersionString, TargetBigEndian, TargetCarbon, TargetHasGUI, TargetLinux, TargetLittleEndian, TargetMacOS, TargetMacOSClassic, TargetMachO, TargetWin32 constants.