【发布时间】: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