Link to this posting

Postby Ursego » 19 Feb 2013, 21:29

The function iif(<boolean expression>, <1st value>, <2nd value>) returns the 1st value if the boolean expression is TRUE; otherwise, it returns the 2nd value.

It works similar to the ternary operator in C/C++/C#/Java (<cond> ? <if true> : <if false>; ) and the IIf function in Visual Basic (as you can read there, "IIf" stands for "Internal If"):

Code: Select all
ls_greeting = iif(lc_sex = "M", "Mr", "Ms")

This approach is very convenient - it helps to keep scripts shorter by avoiding an unnecessary if...else construction. Each use of iif() removes 4 extra lines from the script. That's how the fragment would be looking like without iif():

Code: Select all
if lc_sex = "M" then
   ls_greeting = "Mr"
   ls_greeting = "Ms"
end if

CAUTION, especially if you came from C/C++/C#/Java! If you pass an expression as the 2nd or 3rd argument, that expression is evaluated before being sent to the function (no matter what the result of the Boolean expression is), so DON'T USE iif() as a protection against things like zero divide or reading from a not-existing row in a DataWindow (as you would do using the ternary operator in a language with C-like syntax):

Code: Select all
ll_result = iif(ll_2 <> 0, ll_1 / ll_2, 0) // runtime error if ll_val_2 = 0!!!
ls_name = iif(ll_row > 0, dw_test.object.name[ll_row], "No name") // runtime error if ll_row = 0!!!


1. Save the file spy.pbl on your hard disk (in the folder where the PBLs are stored). The function is in that PBL.
2. Add it to your application's library list.

The function is overloaded for the following datatypes: string, long, decimal, date, time, datetime, PowerObject. To see the overloads, open the function in the "Edit Source" mode. If you are surprised that a global function is overloaded then read here.
User avatar
Site Admin
Posts: 141
Joined: 19 Feb 2013, 20:33

Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!

Traffic Counter

eXTReMe Tracker