Mythran said:
The IIf function doesn't do anything special...
How would you write a function named IIf?
I bet something like:
Public Function IIF(ByVal Expression As Boolean, ByVal ReturnTrue As Object,
ByVal ReturnFalse As Object) As Object
If Expression
Return ReturnTrue
Else
Return ReturnFalse
End If
End Function
<clip>
That's exactly the way I imagine it. And there lies the source of the
issues I mentioned.
Consider:
If Flag Then
AString = BString & CString
Else
AString = CString & BString
End If
Versus:
AString=Iif(Flag, BString & CString, CString & BString)
In the If/Then/Else/EndIf version, *one* string concatenation is
performed, regardless of whether Flag is True or False. But in the Iif
version, *two* string concatenations are performed, as both the
ReturnTrue and ReturnFalse parameters must be fully evaluated before
being passed to the Iif function for selection.
Normally this doesn't make much difference; but it can if you've got
some slow-evaluating expressions, or you're trying to squeeze every
possible bit of speed out of an inner loop.
And that's also the reason why this code will handle Divisor = 0:
If Divisor <> 0 Then
TextBox1.Text = Dividend / Divisor
Else
TextBox1.Text = "Overflow"
End If
And this seemingly equivalent code will fail:
TextBox1.Text = Iif(Divisor <> 0, Dividend / Divisor, "Overflow")