Strange behaviour with if statement

  • Thread starter Thread starter tborn
  • Start date Start date
T

tborn

Hi there,
Not sure if any one has experienced this before and can tell me what's
wrong with this statement:


if verified = false then dataObjects.HasError = true


This is all on one line and verified is defined as a boolean variable.
Irrespective if verified is set to true or false in ALWAYS executes
dataObjects.HasError = true.


Following code works as expected and dataObjects.HasError = true
executes only if verified is set to false.


if verified = false then
dataObjects.HasError = true
end if


To top it all of, I use the construct



if [expression] then [do something]
at other places in the program and it works fine.
Any ideas?


Thomas Born
 
Hi there,
Not sure if any one has experienced this before and can tell me what's
wrong with this statement:


if verified = false then dataObjects.HasError = true


This is all on one line and verified is defined as a boolean variable.
Irrespective if verified is set to true or false in ALWAYS executes
dataObjects.HasError = true.


Following code works as expected and dataObjects.HasError = true
executes only if verified is set to false.


if verified = false then
dataObjects.HasError = true
end if


To top it all of, I use the construct



if [expression] then [do something]
at other places in the program and it works fine.
Any ideas?


Thomas Born

Are you sure that it is actually executing? It seems there was a problem
with the debuger and single line if statements... Basically, it gets out
of step and it looks like it is executing the statement, but in reallity
it's not. Check the values, before and after you step to confirm.
 
Yeah, I've see that exact weirdness in the debugger myself. (It doesn't
actaully execute it).

I almost pulled my hair out trying to debug this line of code:

Try
...
Finally
If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open Then
cn.Close()
End Try

:^)

Greg

Tom Shelton said:
Hi there,
Not sure if any one has experienced this before and can tell me what's
wrong with this statement:


if verified = false then dataObjects.HasError = true


This is all on one line and verified is defined as a boolean variable.
Irrespective if verified is set to true or false in ALWAYS executes
dataObjects.HasError = true.


Following code works as expected and dataObjects.HasError = true
executes only if verified is set to false.


if verified = false then
dataObjects.HasError = true
end if


To top it all of, I use the construct



if [expression] then [do something]
at other places in the program and it works fine.
Any ideas?


Thomas Born

Are you sure that it is actually executing? It seems there was a problem
with the debuger and single line if statements... Basically, it gets out
of step and it looks like it is executing the statement, but in reallity
it's not. Check the values, before and after you step to confirm.
 
if verified = false then dataObjects.HasError = true


An alternate way to code this line would be to avoid the if altogether:

dataObjects.HasError = (Not verified)

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Hi guys,

I made this test and in my opinion it goes as it should go, where did I go
wrong?

Cor
\\\
public class doedoe
Public shared sub Main
Dim dataobjects As New mycl
Dim verified As Boolean = True
If verified = False Then dataobjects.HasError = True
If Not dataobjects Is Nothing AndAlso dataobjects.HasError = False
Then
MessageBox.Show("should be right")
End If
End Sub
End Class
Public Class mycl
Private mHasError As Boolean
Public Property HasError() As Boolean
Get
Return mHasError
End Get
Set(ByVal Value As Boolean)
mHasError = Value
End Set
End Property
///
 
Your example didn't do it for me either, but mine does:

(this code makes no sense, just an example)

Imports System.Data.SqlClient

Public Class doedoe
Public Shared Sub Main()

Dim cn As New
SqlConnection("server=(local);Trusted_Connection=true;database=pubs;")

Try
cn.Open()
cn.Close() ' make sure it is closed before entering finally
block (if no error, that is)
Finally
' debugger will move to cn.Close() even though it is not Open
If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open
Then cn.Close()
End Try

End Sub
End Class

Greg
 
Greg
I tried this and no strange results

\\\
Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\whatever.mdb")
cn.Open()
cn.Close()
Try
Catch ex As Exception
Finally
If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open
Then
MessageBox.Show("Is showed when cn.close is disabled")
End If
End Try
///
 
No, you got to do my example. The problem is when If Then is one line, no
End If.

You won't be able to throw in a messagebox like you are trying. You will
have to step through with the debugger.

If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open Then cn.Close()
<-- all one line


Greg
 
Greg,

I changed it to this,

\\\
Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\whatever.mdb")
cn.Open()
cn.Close()
Try
Catch ex As Exception
Finally
If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open
Then cn.Close()
End Try
///
I have a different behaviour when I disable that first close.

Cor
 
Greg,

It was exactly as you said yesterday what you said and thought that I got
not that behaviour, tomorrow I get it, with this as well,
\\\
Try
Catch ex As Exception
Finally
If 0 = 1 Then MessageBox.Show("hello")
End Try
///
And only in the Finnally block, and it will not be exectuted, it shows it is
executed.

In your example it can be the right situation when this error on the open.
However in this case, when I set it in a three line If statement, it behaves
in another way as you said, crazy.

Cor
 
Cor,

I was going to write back and say that the Finally block had nothing to do
with it. (Just happened to be in my example). But when I tried it without
the Finally, it doesn't do it! It is CRAZY! :^)

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top