About conditional compilation

In PowerBuilder 11.0, you can use the number sign (#) at the start of a line or block of code to mark the code for specialized processing prior to PowerScript compilation. Each line of code or block of conditional code set off by a leading number sign is automatically parsed by a PowerBuilder preprocessor before it is passed to the design-time PowerScript compiler or the PowerBuilder-to-C# (pb2cs) compiler.

Preprocessing symbols

There are six default code-processing symbols that affect the code passed to the PowerScript compiler at design time. Four of these symbols correspond to different PowerBuilder target types, one applies to all .NET target types, and one applies to both PowerScript and .NET target types.

The preprocessor enables PowerBuilder to compile project code specific to a particular deployment target without hindering the compiler’s ability to handle the same code when a different deployment target is selected.

The preprocessor substitutes blank lines for all declarative statements and conditional block delimiters having leading number sign characters before passing the code to the PowerScript compiler or the pb2cs compiler. The contents of the conditional blocks are converted to blank lines depending on which compiler is called upon to process the code handed off by the preprocessor.

Table 13-1 displays the default preprocessing symbols, the project types to which they correspond, and their effects on the code passed to the PowerScript compiler engine or the pb2cs compiler.

Table 13-1: Default preprocessing symbols for conditional compilation

Preprocessing symbols

Project type

Code in this processing block

PBNATIVE

PowerBuilder client-server or distributed applications

Fully parsed by the PowerScript compiler. It is converted to blank lines for the pb2cs compiler.

PBWEBFORM

.NET Web Forms application

Fully parsed by the pb2cs compiler for .NET Web Forms targets only. It is converted to blank lines for the PowerScript compiler and all other types of .NET targets.

PBWINFORM

.NET Windows Forms applications

Fully parsed by the pb2cs compiler for .NET Windows Forms targets only. It is converted to blank lines for the PowerScript compiler and all other types of .NET targets.

PBWEBSERVICE

.NET Web Service component targets

Fully parsed by the pb2cs compiler for .NET Web Service targets only. It is converted to blank lines for the PowerScript compiler and all other types of .NET targets.

PBDOTNET

.NET Web Forms and Windows Forms applications, and .NET Assembly and .NET Web Service components

Fully parsed by the pb2cs compiler for all .NET target types. It is converted to blank lines for the PowerScript compiler.

DEBUG

PowerScript targets and all .NET application and component targets

When a project’s Enable DEBUG Symbol check box is selected, code is fully parsed in deployed applications by the PowerScript compiler, or for .NET targets, by the pb2cs compiler. Code is converted to blank lines when the check box is cleared.

Conditional syntax

You indicate a conditional block of code with a statement of the following type, where symbolType is any of the symbols defined by PowerBuilder:

#IF defined symbolType then

You can use the NOT operator to include code for all target types that are not of the symbol type that you designate. For example, the following code is parsed for all targets that are not of the type PBNative:

#IF NOT defined PBNATIVE then

You can also use #ELSE statements inside the code block to include code for all target types other than the one defined at the start of the code block. You can use #ELSEIF defined symbolType then statements to include code for a specific target type that is different from the one defined at the start of the code block.

The closing statement for a conditional block is always:

#END IF

Comments can be added to conditional code if they are preceded by double slash marks ( // ) in the same line of code. Although you cannot use the PowerScript line continuation character ( & ) in a conditional code statement, you must use it in code that you embed in the conditional block when you use more than one line for a single line of code.

Limitations and error messages

Conditional compilation is not supported in DataWindow syntax, in structure or menu objects, or in JSP targets. You cannot edit the source code for an object to include conditional compilation blocks that span function, event, or variable definition boundaries.

You must rebuild your application after you add a DEBUG conditional block.

Table 13-2 shows the types of error messages displayed for incorrect conditional compilation code.

Table 13-2: Types of error messages returned by the preprocessor

Error message

Description

Invalid if statement

#if statement without a defined symbol, with an incorrectly defined symbol, or without a then clause

#end if directive expected

#if statement without an #end if statement

Unexpected preprocessor directive

Caused by an #else, #elseif, or #end if statement when not preceded by an #if statement

Preprocessor syntax error

Caused by including text after an #else or #end if statement when the text is not preceded by comment characters (//)