【发布时间】:2010-06-16 07:50:48
【问题描述】:
if then else 语句的两边都在执行?
我尝试注释掉真实的一面并将条件移动到具有相同结果的单独变量中。但是,如果我将条件显式设置为 1=0 或 1=1,那么 if then 语句将按照我的预期运行。即只执行等式的一侧...
我唯一一次看到这种事情是编译器崩溃并且不再编译(没有明显的迹象表明它不是)但我以相同的结果重新启动了工作室,我清理了解决方案,构建和重建没有改变?
请告诉我我犯的愚蠢错误 如果重要,请使用 vs2005。
Dim dset As DataSet = New DataSet
If (CboCustomers.SelectedValue IsNot Nothing) AndAlso (CboCustomers.SelectedValue <> "") Then
Dim Sql As String = "Select sal.SalesOrderNo As SalesOrder,cus.CustomerName,has.SerialNo, convert(varchar,sal.Dateofpurchase,103) as Date from [dbo].[Customer_Table] as cus " & _
" inner join [dbo].[Hasp_table] as has on has.CustomerID=cus.CustomerTag " & _
" inner join [dbo].[salesorder_table] as sal On sal.Hasp_ID =has.Hasp_ID Where cus.CustomerTag = '" & CboCustomers.SelectedValue.ToString & "'"
Dim dap As SqlDataAdapter = New SqlDataAdapter(Sql, FormConnection)
dap.Fill(dset, "dbo.Customer_Table")
DGCustomer.DataSource = dset.Tables("dbo.Customer_Table")
Else
Dim erm As String = "wtf"
End If
编辑:我发现这与我使用的发布配置设置有关,我猜它的优化位。有谁知道 vs 的任何实用程序/插件是否显示一条线是否已被优化。 delphi,我以前的语言在左边距显示蓝点表示它是一个编译行,没有点表示它没有编译,vs有没有类似的东西?
或者,有人可以解释优化如何影响这个简单的 if 语句,使其同时运行吗?
EDIT2:使用此线程作为可能的原因/解决方案:Bug only occurring when compile optimization enabled。 对于 x86、x64 和 AnyCPU 上的 release = 优化也是如此 关闭优化。 我在 x64 win7 机器上使用 V2005,如果这很重要的话。
EDIT3:继Chris 注释后,if 语句反编译为
If ((Not Me.CboCustomers.SelectedValue Is Nothing) And (Not Me.CboCustomers.SelectedValue Is "")) Then
所以我现在比以前更加困惑,因为这对我来说看起来很合理......
Edit4:erm 不是一个答案,而是它的作品,所以我现在宣布胜利并继续前进。如果有人知道不留下这个空的try catch的理由,请告诉我。或者如果有人能告诉我为什么 if 语句没有按预期工作,优化开启更好。
Try
If ((Not Me.CboCustomers.SelectedValue Is Nothing) And (Not Me.CboCustomers.SelectedValue Is "")) Then
Dim Sql As String = "Select sal.SalesOrderNo As SalesOrder,cus.CustomerName,has.SerialNo, convert(varchar,sal.Dateofpurchase,103) as Date from [dbo].[Customer_Table] as cus " & _
" inner join [dbo].[Hasp_table] as has on has.CustomerID=cus.CustomerTag " & _
" inner join [dbo].[salesorder_table] as sal On sal.Hasp_ID =has.Hasp_ID Where cus.CustomerTag = '" & CboCustomers.SelectedValue.ToString & "'"
Dim dap As SqlDataAdapter = New SqlDataAdapter(Sql, FormConnection)
dap.Fill(dset, "dbo.Customer_Table")
DGCustomer.DataSource = dset.Tables("dbo.Customer_Table")
Else
CboCustomers.SelectedIndex = 0
End If
Catch ex As Exception
' CODE REVIEW NOTE: MJR 16/06/2010
' 1) This is very odd, i cant figure this out but this hack works.
' 2) The try catch is here purely to make the if statement work as expected?!?!?
' 3) Take it out and both blocks of the if then else will execute in release mode,
' at first i though "DGCustomer.DataSource = dset.Tables("dbo.Customer_Table")" raised an exception but ex is nothing.
' 4) Ideas anyone?
End Try
我投票赞成克里斯,因为他链接的反编译器工具比我拥有的免费工具和它的好建议要好。也赞成 Ho1,因为建议很简单,在走远之前重新启动 IDE 总是一个好主意,但我暂时忘记了。
感谢大家的宝贵时间和建议,如果您确实弄清楚了真正的原因,请务必发表解释
干杯
【问题讨论】:
-
那么,你的意思是代码执行both执行数据绑定的块,以及分配字符串变量的
Else块? -
是的,两者都执行。显然 else 块只是我输入的一段代码,试图弄清楚发生了什么,但是是的,双方!?>!?
-
毫无疑问,我们必须解决错误,但这不是我可以忍受的。我在答案中添加了更精细和更多的诊断代码,以帮助追踪错误。我期待找出导致问题的原因。
标签: vb.net