如果然后声明条件被优化忽略

时间:2010-06-16 07:50:48

标签: vb.net

我觉得我很生气,但有些人可以告诉我什么是我失踪的,它一定是一些愚蠢的简单,我只是看不到树木。

这个if if else语句的两个方面正在执行吗?

我试过评论真实的一面并将条件移动到具有相同结果的单独变量。但是,如果我明确地将条件设置为1 = 0或1 = 1,则if then语句按照我的预期运行。即仅执行等式的一侧......

我见过这种事情的唯一一次是当编译器崩溃并且不再编译时(没有可见的迹象表明它没有)但是ive重新启动了具有相同结果的工作室,我清理了解决方案,构建并重建了没有改变?

请告诉我我犯的愚蠢错误 如果重要的话,使用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 = optimisations,它也是如此 消除优化后离开。 我在x64 win7机器上使用V2005,如果重要的话。

EDIT3:继Chris评论之后,if语句被反编译为

If ((Not Me.CboCustomers.SelectedValue Is Nothing) And (Not Me.CboCustomers.SelectedValue Is "")) Then

所以我现在比以前更加困惑,因为它对我来说看起来很合理......

Edit4:呃不是答案,但是它的作用让我宣布胜利并继续前进。如果有人知道不留下这个空的尝试捕获的原因请告诉我。或者更好的是,如果有人能告诉我为什么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总是一个好主意,而且我忘了一会儿。

感谢所有人的时间和建议,如果你碰巧弄清楚真正的原因,请一定要发表解释

干杯

4 个答案:

答案 0 :(得分:2)

为确保不会多次调用它,请添加一些Diagnostics.Debug.WriteLine语句

  1. 在if之前的一个
  2. 在if
  3. 一个在其他内部
  4. 一个结束后如果
  5. 这4个一起会给你一个很好的时间表,告诉他们如何被召唤。

    我有一些实例(虽然很少和很远)我实际上必须重启机器才能使Visual Studio正常运行。

答案 1 :(得分:2)

这不是一个真正的答案,但显然我没有代表发表评论。它只是一个调试建议......

我想到的一个问题就是接受编译后的代码并用http://www.red-gate.com/products/reflector/之类的东西对其进行反编译。这应该有希望向您展示编译后的if语句。

答案 2 :(得分:0)

我无法解释您遇到问题的原因。如果我正确理解了你问题的这一部分:

  

但是,如果我明确设置了   条件为1 = 0或1 = 1然后是if   然后声明像我一样运作   期望。即仅执行一侧   等式...

你所做的是:

Dim dset As DataSet = New DataSet
If 1 = 1 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

代码按预期工作。我的建议是以下列方式重构If语句:

If Me.CustomerHasBeenSelected() Then
    Dim erm As String = "it worked!"
Else
    Dim erm As String = "wtf"
End If

Private Function CustomerHasBeenSelected() As Boolean
    Return (CboCustomers.SelectedValue IsNot Nothing) AndAlso (CboCustomers.SelectedValue <> "")
End Function

希望这有帮助。

PS。如果它没有解决问题,那么你可以更进一步协助调试。

Dim selected As Boolean = Me.CustomerHasBeenSelected()

If selected Then
    Dim erm As String = "it worked!"
Else
    Dim erm As String = "wtf"
End If

修改1

截至2010年6月17日,Matma仍然遇到原始问题,并且在问题中添加了Try ... End Try块。不是我能忍受的东西。我将采取的下一步是分解if语句,特别是CustomerHasBeenSelected方法,甚至进一步用于调试目的。这就是我现在编写代码的方式。

Dim selected As Boolean = Me.CustomerHasBeenSelected()
Dim crazyBug As Integer = 0

If selected Then

    crazyBug += 1

    ... the real code ...

Else

    crazyBug += 1

    ... the real code ...

End If

If crazyBug = 0 Then
    Throw New Exception("Neither side of the If statement ran.")
ElseIf crazyBug > 1 Then
    Throw New Exception("Both sides of the If statement ran.")
End If

Private Function CustomerHasBeenSelected() As Boolean

    ' There is a crazy bug with the if statement that calls this method so
    ' using multiple if statements to help tracked down the problem.

    If (CboCustomers.SelectedValue Is Nothing)
        Return False
    End If

    If (CboCustomers.SelectedValue <> "")
        Return True
    End If

    Return False

End Function

答案 3 :(得分:0)

问题仅在调试时出现?如果从命令行运行它,问题就会消失。如果是这样,请删除包含调试信息的解决方案suo文件。此信息和文件可能已损坏。我遇到了同样的问题。