Skip to content

Using the new If operator

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:

Dim resultText As String
resultText = If(itemCount > 1, _
    Str(itemCount) + " items were found", _
    "1 item was found")

This displays an error if one occurred:

MsgBox(If(db.Error, "Error: db.ErrorMessage", "no errors")

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:

Dim value As Integervalue = If (value > 10, "Large", 42)

Download Xojo 2014 Release 1 today to start taking advantage of the new If operator!

Questions about other Xojo features? Follow us on Twitter (@xojo) and ask us!