1

Using the code below, why does the iif statement not set the permissionFlag to true, but the if statement does? They should be the same exact logic and I do not understand.

I have written some code below to demonstrate my issue. Copy and paste it into asp vb code behind and set the break point where noted in the comment in the code. I made this very easy to replicate.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    '' create a datatable: here is what the table will 
    '' consist of
    '' col1     col2
    '' 1        1
    '' 2        1
    '' 3        1
    '' 4        1

    Dim dt As New DataTable

    dt.Columns.Add("col1")
    dt.Columns.Add("col2")

    Dim dr As DataRow

    For i As Integer = 1 To 4

        dr = dt.NewRow

        dr(0) = i
        dr(1) = 1

        dt.Rows.Add(dr)

    Next

    Dim permissionFlag As Boolean = False

    ''loop through every row in the table we just created,
    ''and if the 2nd column is = 0 then the permissionFlag
    ''will be false, else the permissionFlag will be true

    For Each drow As DataRow In dt.Rows

        ''check the 2nd column, this is a 1 if user has the permission
        ''but for some reason, permissionFlag still winds up false after 
        ''this runs.

        ''***************** place breakpoint here to check value of
        ''***************** permissionFlag after each if statement

        IIf(CInt(drow(1)) = 0, permissionFlag = False, permissionFlag = True)

        ''this next if statement is the same logic as the above, except 
        ''the above iif statement is not working correctly, and this one is.
        ''permissionFlag is scwitched to true after this statement runs
        ''i don't know why, they both look correct to me.

        If CInt(drow(1)) = 0 Then
            permissionFlag = False
        Else
            permissionFlag = True
        End If

    Next

End Sub
Taylor Brown
  • 1,689
  • 2
  • 17
  • 33

2 Answers2

4

Iif is a function that takes three arguments. If is a statement with separate parts.

When you call

IIf(CInt(drow(1)) = 0, permissionFlag = False, permissionFlag = True)

You're actually evaluating all three arguments, so you're setting permissionFlag to false and then setting it immediately to true.

With the If statement, VB.NET is executing only one or the other conditions.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • 2
    +1 for being the only one to mention that "IIf" is a function. It might clarify the behavior to the OP to mention that methods/functions *always* evaluate all arguments passed to it. The "If" operator is a totally different beast. – Dave Doknjas Aug 26 '14 at 22:53
4

Basically, you aren't using IIf correctly here. It doesn't use short circuit evaluation therefore both expressions are evaluated meaning your code is the equivalent to

permissionFlag = False
permissionFlag = True

If you want to use IIf in this situation then you need to set permissionFlag to be the result of the returned expression e.g.

permissionFlag = IIf(CInt(drow(1)) = 0, False, True)

However, it's not really required at all, here's a shorter way

permissionFlag = CInt(drow(1)) != 0
James
  • 80,725
  • 18
  • 167
  • 237
  • That makes sense, but after the iif function runs in the code i provided the permissionFlag is actually set to false, not true, which is still a little confusing, unless the order is based on the result of the expression, in which case the "false part" would run first in my code, and then the "true part?" – Taylor Brown Aug 27 '14 at 13:51
  • @taybriz perhaps that is the case, I presumed that they would be evalualted in the order they are passed ("true" part then "false" part). It could possibly be an undocumented, compiler specific, process. – James Aug 27 '14 at 13:54