Search:
Namespaces
Discussions
.NET v1.1
Feedback
Strange behaviour with if statement
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.vb
.
Post a new message to this list...
tborn@hlra.com.au
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
Reply to this message...
Tom Shelton
On 8 Sep 2004 20:11:58 -0700,
Click here to reveal e-mail address
wrote:
[Original message clipped]
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.
--
Tom Shelton [MVP]
Reply to this message...
Greg Burns
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" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Chris Dunaway
On 8 Sep 2004 20:11:58 -0700,
Click here to reveal e-mail address
wrote:
> 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.
Reply to this message...
Cor Ligthert
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
///
Reply to this message...
Greg Burns
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
"Cor Ligthert" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Cor Ligthert
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
///
Reply to this message...
Greg Burns
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
"Cor Ligthert" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Cor Ligthert
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
[Original message clipped]
Reply to this message...
Greg Burns
Not sure what you mean by that.
I just tried you exact code and it behaves the same whether I disable the
first close or not.
Understand, nobody is disputing that if the connection is already closed it
will NOT try and close it again. The code works properly.
It is simpy this; if you step through with the debugger it ALWAYS highlights
that cn.Close on the other side of the Then. It appears that it is
exectuing it, whether it is or not.
Make sense? I added a screen shot (and simplified the If expression)
Greg
"Cor Ligthert" <
Click here to reveal e-mail address
> wrote in message
news:OWIc$
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Cor Ligthert
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
[Original message clipped]
Reply to this message...
Greg Burns
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
"Cor Ligthert" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
System.Data.ConnectionState
System.Data.OleDb.OleDbConnection
System.Data.SqlClient.SqlConnection
System.Windows.Forms.MessageBox
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com