5 Replies - 1931 Views - Last Post: 14 May 2014 - 05:33 AM

#1 depricated  Icon User is offline

  • DLN-000

Reputation: 662
  • View blog
  • Posts: 2,300
  • Joined: 13-September 08

if() and iif(), when might we use the latter?

Posted 12 May 2014 - 07:46 AM

For context if you're not immediately familiar with them, if and iif are essentially ternary operators.

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


Is This A Good Question/Topic? 0
  • +

Replies To: if() and iif(), when might we use the latter?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: if() and iif(), when might we use the latter?

Posted 12 May 2014 - 08:15 AM

Just to provide some references for this discussion:

If Operator :MSDN

If() can be supplied with just two arguments; in which case, it is the equivalent to C#'s null coalescing operator (??).
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: if() and iif(), when might we use the latter?

Posted 12 May 2014 - 10:25 AM

IIF( boolExpr, trueExpr, falseExpr )

Evaluates both sides of the outcome before choice which one to follow. This is preserve compatibility with the VB6 semantic meaning.

IF( boolExpr , trueExpr, falseExpr )
Only evaluates the result after deciding which to take.
Was This Post Helpful? 0
  • +
  • -

#4 depricated  Icon User is offline

  • DLN-000

Reputation: 662
  • View blog
  • Posts: 2,300
  • Joined: 13-September 08

Re: if() and iif(), when might we use the latter?

Posted 13 May 2014 - 11:56 AM

Hm, maybe I was unclear.

I was wondering if anyone could think of an instance where we might want that evaluation before deciding which to use, rather than after.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: if() and iif(), when might we use the latter?

Posted 13 May 2014 - 01:00 PM

IIf() is, effectively, deprecated. Besides which, we should avoid complex statements that have, or rely upon, side-effects.

So, personally, I think it is more be important to be aware of the differences, and to use If() in preference to IIf().

(Probably not the response you are looking for ;))
Was This Post Helpful? 0
  • +
  • -

#6 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: if() and iif(), when might we use the latter?

Posted 14 May 2014 - 05:33 AM

i think My example explain what you need
        Dim Rowcount As Integer
        If Table IsNot Nothing AndAlso Table.Rows.Count > 0 Then
            Rowcount = Table.Rows.Count
        Else
            Rowcount = 0
        End If
        Rowcount = IIf(Table IsNot Nothing AndAlso Table.Rows.Count > 0, Table.Rows.Count, 0)



in the above snippet look like both if and iif are doing the same but in real when you run this code
IIF will raise an error
reason
if block is executed step by step first the expression then based on the expression either true part or false part
where as IIF is executed at a single stroke,(i.e) IIF evaluate the entire expression
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1