Although the remarks above state that the IIf function is a counterpart the for C++ (and C#) ternary conditional operator (?:); it has does not have the same semantics.
For example, have a look at the following C# method which returns the length of a string:
public int GetLength(string value)
{ return string.IsNullOrEmpty(value) ? 0 : value.Length;
}
As you can see, if value is a null reference (Nothing in Visual Basic) or is empty, then the method returns 0; otherwise, it returns the length of the value parameter.
You might be tempted to write the same code in Visual Basic like so (with Option Strict On):
Public Function GetLength(ByVal value As String) As Integer
Return CInt(IIf(value Is Nothing, 0, value.Length))
End Function
However, unlike the ternary conditional operator above, IIf is simply a function call where both the true part and the false part are evaluated whether the expression returns true or false. This means that at runtime when Nothing is passed for the parameter value, a NullReferenceException will be thrown when the false part attempts to retrieve value.Length.
Instead of using IIf, you will need to do the following to achieve the sementics as the C# code:
Public Function GetLength(ByVal value As String) As Integer
If (value Is Nothing) Then
Return 0
End If
Return value.Length
End Function