If Then Statement Condition Being Ignored With Optimisations On

Posted by Matma on Stack Overflow See other posts from Stack Overflow or by Matma
Published on 2010-06-16T07:50:48Z Indexed on 2010/06/16 9:02 UTC
Read the original article Hit count: 261

Filed under:

I think im going mad but can some show me what im missing, it must be some stupidly simple i just cant see the wood for the trees.

BOTH side of this if then else statement are being executed?

Ive tried commenting out the true side and moving the condition to a seperate variable with the same result. However if i explicitly set the condition to 1=0 or 1=1 then the if then statement is operating as i would expect. i.e. only executing one side of the equation...

The only time ive seen this sort of thing is when the compiler has crashed and is no longer compiling (without visible indication that its not) but ive restarted studio with the same results, ive cleaned the solution, built and rebuilt with no change?

please show me the stupid mistake im making using vs2005 if it matters.

    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

EDIT: i have found that this is something to do with the release config settings im using, i guesing its the optimisations bit. does anyone know of any utils/addons for vs that show if a line has been optimised out. delphi, my former language showed blue dots in the left margin to show that it was a compiled line, no dot meaning it wasnt compiled in, is there anything like that for vs?

alternatively can someone explain how optimisations would affect this simple if statement causeing it to run both sides?

EDIT2: using this thread as possible causes/solutions : http://stackoverflow.com/questions/2135509/bug-only-occurring-when-compile-optimization-enabled. It does the same with release = optimisations on, x86, x64 and AnyCPU Goes away with optimisations off. Im using V2005 on a x64 win7 machine, if that matters.

Thanks

© Stack Overflow or respective owner

Related posts about vb.net