【问题标题】:IIf statement and If statement evaluating differently. IIf statement yields incorrect results. Why?IIf 语句和 If 语句的评估方式不同。 IIf 语句产生不正确的结果。为什么?
【发布时间】:2014-10-20 09:26:06
【问题描述】:

使用下面的代码,为什么 iif 语句没有将 permissionFlag 设置为 true,而 if 语句却设置了?它们应该是相同的确切逻辑,我不明白。

我在下面编写了一些代码来演示我的问题。将其复制并粘贴到后面的 asp vb 代码中,并在代码注释中注明的位置设置断点。我让这个很容易复制。

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

【问题讨论】:

    标签: asp.net vb.net if-statement iif


    【解决方案1】:

    基本上,您在这里没有正确使用IIf。它不使用短路评估,因此 两个 表达式都被评估,这意味着您的代码等效于

    permissionFlag = False
    permissionFlag = True
    

    如果你想在这种情况下使用 IIf,那么你需要将 permissionFlag 设置为 returned 表达式的结果,例如

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

    但是,它根本不是必需的,这里有一个更短的方法

    permissionFlag = CInt(drow(1)) != 0
    

    【讨论】:

    • 这是有道理的,但是在 iif 函数在代码中运行之后,我提供的 permissionFlag 实际上设置为 false,而不是 true,这仍然有点令人困惑,除非顺序是基于结果的表达式,在这种情况下,“假部分”将首先在我的代码中运行,然后是“真部分?”
    • @taybriz 也许是这样,我假设它们会按照传递的顺序进行评估(“true”部分然后是“false”部分)。它可能是一个未记录的、特定于编译器的过程。
    【解决方案2】:

    Iif 是一个接受三个参数的函数。 If 是一个包含不同部分的语句。

    当你打电话时

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

    您实际上是在评估所有三个参数,因此您将 permissionFlag 设置为 false,然后立即将其设置为 true。

    使用If 语句,VB.NET 只执行一个或其他条件。

    【讨论】:

    • +1 是唯一一个提到“IIf”是一个函数的人。它可能会向 OP 澄清行为,以提及方法/函数 总是 评估传递给它的所有参数。 “If”运算符是完全不同的野兽。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多