Using the new If operator

Published March 12, 2014 by
Paul Lefebvre

In Xojo 2014 Release 1 there is now an If operator that can return a result.

Everyone has used If statements for controlling program flow.

Rather than controlling program flow, Xojo’s new If operator provides a way for you to get the result of a comparison. You pass it three parameters: a boolean for the conditional test, a value to return if the conditional test evaluates to True and a value to return if the conditional test evaluates to False.

By using the If operator, you can easily do a comparison and get a result in a single line of code. Here is an example that changes the text based on the number of items:

The return type is the common type between the two values. For example, if an Int8 and an Int32 were passed in, the result type would be Int32. Having no common type result in a compile error, you cannot mix things like Strings and Numbers. For example, this generates a “Type Mismatch” compile error: