I recently asked a question about IIf vs. If and found out that there is another function in VB called If which basically does the same thing as IIf
One very important distinct between IIf()
and If()
is that with Option Infer On
the later will implicitly cast the results to the same data type in certain cases, as where IIf
will return Object
.
Example:
Dim val As Integer = -1
Dim iifVal As Object, ifVal As Object
iifVal = IIf(val >= 0, val, Nothing)
ifVal = If(val >= 0, val, Nothing)
Output:
iifVal
has value of Nothing and type of Object
ifVal
has value of 0 and type of Integer, b/c it is implicitly converting Nothing to an Integer.