473,566 Members | 3,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behaviour with if statement

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.Has Error = 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.Has Error = true.
Following code works as expected and dataObjects.Has Error = true
executes only if verified is set to false.
if verified = false then
dataObjects.Has Error = 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

Nov 21 '05 #1
10 1184
On 8 Sep 2004 20:11:58 -0700, tb***@hlra.com. au wrote:
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.Has Error = 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.Has Error = true.
Following code works as expected and dataObjects.Has Error = true
executes only if verified is set to false.
if verified = false then
dataObjects.Has Error = 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.

--
Tom Shelton [MVP]
Nov 21 '05 #2
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" <to*@YOUKNOWTHE DRILLmtogden.co m> wrote in message
news:14******** *************** ******@40tude.n et...
On 8 Sep 2004 20:11:58 -0700, tb***@hlra.com. au wrote:
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.Has Error = 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.Has Error = true.
Following code works as expected and dataObjects.Has Error = true
executes only if verified is set to false.
if verified = false then
dataObjects.Has Error = 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.

--
Tom Shelton [MVP]

Nov 21 '05 #3
On 8 Sep 2004 20:11:58 -0700, tb***@hlra.com. au wrote:
if verified = false then dataObjects.Has Error = true

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

dataObjects.Has Error = (Not verified)

--
Chris

dunawayc[AT]sbcglobal_lunch meat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #4
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.Has Error = True
If Not dataobjects Is Nothing AndAlso dataobjects.Has Error = 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
///

Nov 21 '05 #5
Your example didn't do it for me either, but mine does:

(this code makes no sense, just an example)

Imports System.Data.Sql Client

Public Class doedoe
Public Shared Sub Main()

Dim cn As New
SqlConnection(" server=(local); Trusted_Connect ion=true;databa se=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" <no**********@p lanet.nl> wrote in message
news:up******** ******@TK2MSFTN GP12.phx.gbl...
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.Has Error = True
If Not dataobjects Is Nothing AndAlso dataobjects.Has Error = 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
///

Nov 21 '05 #6
Greg
I tried this and no strange results

\\\
Dim cn As New OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data
Source=C:\whate ver.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
///
Nov 21 '05 #7
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" <no**********@p lanet.nl> wrote in message
news:Ol******** ******@TK2MSFTN GP12.phx.gbl...
Greg
I tried this and no strange results

\\\
Dim cn As New OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data
Source=C:\whate ver.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
///

Nov 21 '05 #8
Greg,

I changed it to this,

\\\
Dim cn As New OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data
Source=C:\whate ver.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
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" <no**********@p lanet.nl> wrote in message
news:Ol******** ******@TK2MSFTN GP12.phx.gbl...
Greg
I tried this and no strange results

\\\
Dim cn As New OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data Source=C:\whate ver.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
///


Nov 21 '05 #9
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

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" <no**********@p lanet.nl> wrote in message
news:OW******** ******@TK2MSFTN GP12.phx.gbl...
Greg,

I changed it to this,

\\\
Dim cn As New OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data Source=C:\whate ver.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
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" <no**********@p lanet.nl> wrote in message
news:Ol******** ******@TK2MSFTN GP12.phx.gbl...
> Greg
> I tried this and no strange results
>
> \\\
> Dim cn As New

OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data
> Source=C:\whate ver.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
> ///
>
>



Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1429
by: Markus Franz | last post by:
Hi. Today I created a script called load.py for using at the command line written in Python 2.3. This script should load as many websites as given on the comand line and print them with a seperation string to stdout. The loading was done in parallel. (I used processes for this.) The script was started by the following command:
2
1467
by: Neil | last post by:
I have a strange situation. I have a stored procedure that is hanging upon execution, but only some machines and not others. The db is an Access 2000 MDB using ODBC linked tables and a SQL 7 back end. The sp is executed as a pass-through. The sp is fairly simple: UPDATE CUSTOMER SET LastMergeName = . FROM (CUSTOMER C INNER JOIN...
3
2335
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
31
2593
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
5
2239
by: Hatul Shilgy | last post by:
Hi I'm facing a very strange problem here. My code looks like this if(A == null return doSomethingWith(A.b) When I run my code (in debug mode), I get a NullReferenceException -- and on breaking, I discover that A is indeed null. (The exception is raised in the doSomethingWith line.
5
3061
by: Praveen_db2 | last post by:
Hi All db2 8.1.3 Windows I have folowing table structures CREATE TABLE tb_RTB( EMP_ID INTEGER, DESC VARCHAR(20)); CREATE TABLE tb_ERROR( SQL_STATE CHAR(5), SQL_DESC VARCHAR(20),
23
1583
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
8
1670
by: matthewperpick | last post by:
Check out this toy example that demonstrates some "strange" behaviour with keyword arguments and inheritance. ================================= class Parent: def __init__(self, ary = ): self.ary = ary def append(self):
8
5282
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code:...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.