The only difference between them to speak of is that iif() evaluates both true and false results, while if only evaluates the returned result. This can lead to some serious bugs that might slip past using iif. For example
Dim left, right As Integer left = inputLeft right = inputRight MsgBox(IIf(left <> 0 And right <> 0, left \ right, "Cannot divide by 0."))
In this example, we've declared that if either left or right are 0, to return "Cannot divide by 0." But if either are set to 0, instead we get a runtime error. This is because iif evaluates both sides of the argument, even though it only needs to evaluate one.
Microsoft provides a similar example on MSDN
number = 12 ' When the divisor is not 0, both If and IIf return 4. Dim divisor = 3 Console.WriteLine(If(divisor <> 0, number \ divisor, 0)) Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0)) ' When the divisor is 0, IIf causes a run-time error, but If does not. divisor = 0 Console.WriteLine(If(divisor <> 0, number \ divisor, 0)) ' Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))
So I imagine there might be a time when this method of evaluation is worthwhile. I thought I'd bring it up for discussion, see what people thought, if you have any examples of where that functionality is taken advantage of or ideas on how one might.
This post has been edited by depricated: 12 May 2014 - 07:47 AM