473,654 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with conditional statements in vb.net IDE

I am getting odd behavior in my code, first I thought it was caused by SP1
but now I have the same issue with a vanilla version of VS 2005. When I
execute a function that contains an if statement. i.e.

Dim b As Boolean
Dim i As Integer = 0

If b then
i += 1 'point 1
msgbox(i.toStri ng()) ' point 2
End If

the problem is even though the condition is false the code enters at point
2... and the messagebox appears... What the hell is up with that? I've look
up to see if there is anything online but can't find any answers. Closing VS
2005 doesn't work... Rebooting doesn't work... Can anyone help!
Jan 25 '07 #1
5 1805
Doesn't do it under my version of VS2005. What else is in the routine
where you're running this?

Robin S.
-------------------------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:94******** *************** ***********@mic rosoft.com...
>I am getting odd behavior in my code, first I thought it was caused by
SP1
but now I have the same issue with a vanilla version of VS 2005. When
I
execute a function that contains an if statement. i.e.

Dim b As Boolean
Dim i As Integer = 0

If b then
i += 1 'point 1
msgbox(i.toStri ng()) ' point 2
End If

the problem is even though the condition is false the code enters at
point
2... and the messagebox appears... What the hell is up with that? I've
look
up to see if there is anything online but can't find any answers.
Closing VS
2005 doesn't work... Rebooting doesn't work... Can anyone help!

Jan 25 '07 #2
This isn't the exact code. It was just an example... It happens on a giving
If statement. The first code I had this problem with was similar to the
previous post... and dealt with managed code.

This is the function I'm having an issue with currently

Public Sub InstallHooks()
If hMouseHook = 0 Then
MouseHookProced ure = New Win32.HookProc( AddressOf MouseHookProc)
hMouseHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_MOU SE_LL, _
MouseHookProced ure, _

Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)), _
0)

If hMouseHook = 0 Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookPro cedure = New Win32.HookProc( AddressOf
KeyboardHookPro c)
hKeyboardHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_KEY BOARD_LL, _
KeyboardHookPro cedure, _

Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)), _
0)

If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetW indowsHookEx failed.") in the last if.

I can post the rest of the code if you want to try too reproduce the
behavior...

Thanks

"RobinS" wrote:
Doesn't do it under my version of VS2005. What else is in the routine
where you're running this?

Robin S.
-------------------------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:94******** *************** ***********@mic rosoft.com...
I am getting odd behavior in my code, first I thought it was caused by
SP1
but now I have the same issue with a vanilla version of VS 2005. When
I
execute a function that contains an if statement. i.e.

Dim b As Boolean
Dim i As Integer = 0

If b then
i += 1 'point 1
msgbox(i.toStri ng()) ' point 2
End If

the problem is even though the condition is false the code enters at
point
2... and the messagebox appears... What the hell is up with that? I've
look
up to see if there is anything online but can't find any answers.
Closing VS
2005 doesn't work... Rebooting doesn't work... Can anyone help!


Jan 26 '07 #3
If I add the lines
If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
Dim i as Integer ' added line
i=0 ' added line
End If
I get a warning that i is an unused variable... I had no warnings or errors
before adding this code... I am getting the same problem with 2 computers
with different configs.

"carbo" wrote:
This isn't the exact code. It was just an example... It happens on a giving
If statement. The first code I had this problem with was similar to the
previous post... and dealt with managed code.

This is the function I'm having an issue with currently

Public Sub InstallHooks()
If hMouseHook = 0 Then
MouseHookProced ure = New Win32.HookProc( AddressOf MouseHookProc)
hMouseHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_MOU SE_LL, _
MouseHookProced ure, _

Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)), _
0)

If hMouseHook = 0 Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookPro cedure = New Win32.HookProc( AddressOf
KeyboardHookPro c)
hKeyboardHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_KEY BOARD_LL, _
KeyboardHookPro cedure, _

Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)), _
0)

If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetW indowsHookEx failed.") in the last if.

I can post the rest of the code if you want to try too reproduce the
behavior...

Thanks

"RobinS" wrote:
Doesn't do it under my version of VS2005. What else is in the routine
where you're running this?

Robin S.
-------------------------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:94******** *************** ***********@mic rosoft.com...
>I am getting odd behavior in my code, first I thought it was caused by
>SP1
but now I have the same issue with a vanilla version of VS 2005. When
I
execute a function that contains an if statement. i.e.
>
Dim b As Boolean
Dim i As Integer = 0
>
If b then
i += 1 'point 1
msgbox(i.toStri ng()) ' point 2
End If
>
the problem is even though the condition is false the code enters at
point
2... and the messagebox appears... What the hell is up with that? I've
look
up to see if there is anything online but can't find any answers.
Closing VS
2005 doesn't work... Rebooting doesn't work... Can anyone help!
Jan 26 '07 #4
i is an unused variable because it will never reach those statements.
When you throw an exception, it exits the routine before executing the
next statements. If you really wanted them to be executed, you would
put in a Try/Catch, and put those in the Finally block.

>even though hKeyboardHook is not 0 it executes Throw New
Exception("Set WindowsHookEx failed.") in the last if.
If you put a breakpoint right on that IF statement, and check the value
of hKeyboardHook, it's not 0?

Robin S.
------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:77******** *************** ***********@mic rosoft.com...
If I add the lines
If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
Dim i as Integer ' added line
i=0 ' added line
End If
I get a warning that i is an unused variable... I had no warnings or
errors
before adding this code... I am getting the same problem with 2
computers
with different configs.

"carbo" wrote:
>This isn't the exact code. It was just an example... It happens on a
giving
If statement. The first code I had this problem with was similar to
the
previous post... and dealt with managed code.

This is the function I'm having an issue with currently

Public Sub InstallHooks()
If hMouseHook = 0 Then
MouseHookProced ure = New Win32.HookProc( AddressOf
MouseHookPro c)
hMouseHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_MOU SE_LL, _
MouseHookProced ure, _

Marshal.GetHIN STANCE(Reflecti on.Assembly.Get ExecutingAssemb ly().GetModules ()(0)),
_
0)

If hMouseHook = 0 Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookPro cedure = New Win32.HookProc( AddressOf
KeyboardHookPr oc)
hKeyboardHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_KEY BOARD_LL, _
KeyboardHookPro cedure, _

Marshal.GetHIN STANCE(Reflecti on.Assembly.Get ExecutingAssemb ly().GetModules ()(0)),
_
0)

If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("Set WindowsHookEx failed.") in the last if.

I can post the rest of the code if you want to try too reproduce the
behavior...

Thanks

"RobinS" wrote:
Doesn't do it under my version of VS2005. What else is in the
routine
where you're running this?

Robin S.
-------------------------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:94******** *************** ***********@mic rosoft.com...
I am getting odd behavior in my code, first I thought it was
caused by
SP1
but now I have the same issue with a vanilla version of VS 2005.
When
I
execute a function that contains an if statement. i.e.

Dim b As Boolean
Dim i As Integer = 0

If b then
i += 1 'point 1
msgbox(i.toStri ng()) ' point 2
End If

the problem is even though the condition is false the code enters
at
point
2... and the messagebox appears... What the hell is up with that?
I've
look
up to see if there is anything online but can't find any answers.
Closing VS
2005 doesn't work... Rebooting doesn't work... Can anyone help!

Jan 26 '07 #5
Yes the value on hKeyboardHook is not zero. I've come to the conclusion that
there is a code error that the compiler is not finding on compilation anyway
I found great C# code on the codeproject that works just used Lutz Roeder's
..NET Reflector to convert it to VB.Net. Code works like a charm. Thanks for
you help, I decided to trash the buggy code and move on with the new one :)
If anybody want the converted code (good code for a GlobalHook) let me know,
I'll post it in it's entirety.

"RobinS" wrote:
i is an unused variable because it will never reach those statements.
When you throw an exception, it exits the routine before executing the
next statements. If you really wanted them to be executed, you would
put in a Try/Catch, and put those in the Finally block.

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetW indowsHookEx failed.") in the last if.

If you put a breakpoint right on that IF statement, and check the value
of hKeyboardHook, it's not 0?

Robin S.
------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:77******** *************** ***********@mic rosoft.com...
If I add the lines
If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
Dim i as Integer ' added line
i=0 ' added line
End If
I get a warning that i is an unused variable... I had no warnings or
errors
before adding this code... I am getting the same problem with 2
computers
with different configs.

"carbo" wrote:
This isn't the exact code. It was just an example... It happens on a
giving
If statement. The first code I had this problem with was similar to
the
previous post... and dealt with managed code.

This is the function I'm having an issue with currently

Public Sub InstallHooks()
If hMouseHook = 0 Then
MouseHookProced ure = New Win32.HookProc( AddressOf
MouseHookProc)
hMouseHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_MOU SE_LL, _
MouseHookProced ure, _

Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)),
_
0)

If hMouseHook = 0 Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookPro cedure = New Win32.HookProc( AddressOf
KeyboardHookPro c)
hKeyboardHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_KEY BOARD_LL, _
KeyboardHookPro cedure, _

Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)),
_
0)

If (hKeyboardHook = 0) Then 'SetWindowsHook Ex failed
RemoveHooks()
Throw New Exception("SetW indowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetW indowsHookEx failed.") in the last if.

I can post the rest of the code if you want to try too reproduce the
behavior...

Thanks

"RobinS" wrote:

Doesn't do it under my version of VS2005. What else is in the
routine
where you're running this?

Robin S.
-------------------------------------------------
"carbo" <ca***@discussi ons.microsoft.c omwrote in message
news:94******** *************** ***********@mic rosoft.com...
>I am getting odd behavior in my code, first I thought it was
>caused by
>SP1
but now I have the same issue with a vanilla version of VS 2005.
When
I
execute a function that contains an if statement. i.e.
>
Dim b As Boolean
Dim i As Integer = 0
>
If b then
i += 1 'point 1
msgbox(i.toStri ng()) ' point 2
End If
>
the problem is even though the condition is false the code enters
at
point
2... and the messagebox appears... What the hell is up with that?
I've
look
up to see if there is anything online but can't find any answers.
Closing VS
2005 doesn't work... Rebooting doesn't work... Can anyone help!



Jan 26 '07 #6

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

Similar topics

49
2593
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon code sample where newbies expect the two function calls below to both print : def f( list= ): print list.append!(1) f() # prints
15
2815
by: marco | last post by:
Hi, I'm a Javascript Newby. But that doesn't discourage me at all. At this time I'm working on a little javascript-gimmick. A white browser page filled with white (invissible for the viewer) text. Every white text character has in case of a MouseOver an other (randomly given) change cursor of one of the 15 different browser cursors).
1
16550
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
92
9843
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if someone helps me... Urgent - Please reply soon ! Thanks, Raghu
50
4175
by: sabarish | last post by:
Hi to all. find out the biggest among two numbers without using any conditional statements and any relational operators.
37
4294
by: priya | last post by:
Hi all, I am using strdup() in my c program..But I am having some pr0blem while using the free() in my c code.here I am pasting the my code. #include <stdio.h>
102
5646
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler can't tell where a bracket is missing.... a few weeks have past, I requested a feature for the delphi ide/editor "automatic identation of code in begin/end statements etc" and today when I woke up I suddenly released a very simple solution for this...
7
1448
by: Stephan Rose | last post by:
Ok here is my scenario I have an interface called IScalar which describes a one dimensional number that has a certain unit of measurement. This interface is used to create multiple structures, each structure representing a certain unit of measurement. So I currently end up with 4 structures each implementing the IScalar interface.
10
3086
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then have blocks of code in different source files that were conditionally compiled based on that constant. Now that C# has done away with include files, is there any way of doing the same thing, short of defining the constant multiple times at the head...
43
7564
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. How is it possible? Please help me. Thanks in advance.
0
8815
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
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.