views:

3631

answers:

1

There are hints of the answer to this question here and there on this site, but I'm asking a slightly different answer.

Where does Crystal Reports document that this syntax does not work?

Trim({PatientProfile.First}) + " "
    + Trim(Iif(
        IsNull({PatientProfile.Middle}) 
        , Trim({PatientProfile.Middle}) + " "
        , " "
        )
    )  
+ Trim({PatientProfile.Last})

I know the solution is

If IsNull({PatientProfile.Middle}) Then
    Trim({PatientProfile.First})
        + " " + Trim({PatientProfile.Last})
Else
    Trim({PatientProfile.First})
       + " " + Trim({PatientProfile.Middle})
       + " " + Trim({PatientProfile.Last})

but how are we supposed to figure out we can't use the first version?

The documentation for IsNull says

  • Evaluates the field specified in the current record and returns TRUE if the field contains a null value

and Iif gives

  • [Returns] truePart if expression is True and falsePart if expression is False. The type of the returned value is the same as the type of truePart and falsePart.

I suppose if you stare at that line about "type of the return value" you can get it, but...

+1  A: 

I think CR evaluates both IIFs true and false parts. Because you have "Trim({PatientProfile.Middle})" part there, which will be evaluated aganst null value, CR formula evaluator seems just fail.

Arvo
Yes, but I'm trying to figure out where in the bloody documentation it _says_ this, instead of making everyone who runs into it Google for the answer. After all, one or two of us know how code is _supposed_ to work...
SarekOfVulcan
From CR help:Every argument of the IIF function is evaluated before the result is returned. Thus, you should watch out for undesirable side effects when using IIF. For example, if falsePart results in division by zero, an error will occur, even if expression is True and so truePart is returned.
Arvo