Reputation: 345
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 : 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.
EDIT3: Following on from Chris comment this is what the if statement decompiled to
If ((Not Me.CboCustomers.SelectedValue Is Nothing) And (Not Me.CboCustomers.SelectedValue Is "")) Then
so im now even more confused than before as it looks reasonable to me...
Edit4: erm not a answer but its works so im declaring victory and moving on for now. if anyone knows a reason not to leave this empty try catch in please tell me. or better still if someone can tell me why the if statement didnt work as expected with optimizations turned on better still.
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
im up voting Chris becuase the decompiler tool he linked is better than the free ones i had and its good advice. Also upvoting Ho1 because simple as the suggestion was, restarting the IDE before going to far is always a good idea and one that i forgot for a moment.
Thanks all for the time and suggestions, if you do happen to figure out the real reason please by all mean post an explanation
Cheers
Upvotes: 1
Views: 2318
Reputation: 4932
I can not explain why you are experiencing the problem you have. If I understand this part of your question correctly:
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...
What you have done is:
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
And the code works as expected. My suggestion would be to refactor the If statement in the following way:
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
Hope this helps.
PS. If it doesn't solve the problem then you could go one step further to assist in debugging.
Dim selected As Boolean = Me.CustomerHasBeenSelected()
If selected Then
Dim erm As String = "it worked!"
Else
Dim erm As String = "wtf"
End If
Edit 1
As at 17 June 2010 Matma is still experiencing the original problem and has a hack of adding the Try ... End Try block around the problem. Not something I could live with. The next step I would take is to break down the if statement, in particular the CustomerHasBeenSelected method, even further for debugging purposes. This is how I would now write the code.
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
Upvotes: 0
Reputation: 21684
Is the problem only when debugging? If you run this from the command line does the problem go away. If so, delete the solutions suo file which contains your debugging information. This information and the file are probably corrupt. I have had this exact same problem.
Upvotes: 0
Reputation: 27609
This isn't really an answer but apparently I dont' have the rep to comment. Its just a suggestion for debugging...
One thougth that crosses my mind is to take teh compiled code and decompile it with something like http://www.red-gate.com/products/reflector/. This should hopefully show you what the if statement looks like after compilation.
Upvotes: 2
Reputation: 55009
Just to make sure that it's not being called multiple times, add some Diagnostics.Debug.WriteLine
statements
Those 4 together would give you a good timeline in how they're being called.
I have had a few instances (though very few and far between) where I actually had to reboot the machine to make Visual Studio behave properly.
Upvotes: 2