473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.toString()) ' 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 1789
Doesn't do it under my version of VS2005. What else is in the routine
where you're running this?

Robin S.
-------------------------------------------------
"carbo" <ca***@discussions.microsoft.comwrote in message
news:94**********************************@microsof t.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.toString()) ' 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
MouseHookProcedure = New Win32.HookProc(AddressOf MouseHookProc)
hMouseHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_MOUSE_LL, _
MouseHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecut ingAssembly().GetModules()(0)), _
0)

If hMouseHook = 0 Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookProcedure = New Win32.HookProc(AddressOf
KeyboardHookProc)
hKeyboardHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_KEYBOARD_LL, _
KeyboardHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecut ingAssembly().GetModules()(0)), _
0)

If (hKeyboardHook = 0) Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetWindowsHookEx 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***@discussions.microsoft.comwrote in message
news:94**********************************@microsof t.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.toString()) ' 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 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx 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
MouseHookProcedure = New Win32.HookProc(AddressOf MouseHookProc)
hMouseHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_MOUSE_LL, _
MouseHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecut ingAssembly().GetModules()(0)), _
0)

If hMouseHook = 0 Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookProcedure = New Win32.HookProc(AddressOf
KeyboardHookProc)
hKeyboardHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_KEYBOARD_LL, _
KeyboardHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecut ingAssembly().GetModules()(0)), _
0)

If (hKeyboardHook = 0) Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetWindowsHookEx 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***@discussions.microsoft.comwrote in message
news:94**********************************@microsof t.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.toString()) ' 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("SetWindowsHookEx 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***@discussions.microsoft.comwrote in message
news:77**********************************@microsof t.com...
If I add the lines
If (hKeyboardHook = 0) Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx 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
MouseHookProcedure = New Win32.HookProc(AddressOf
MouseHookProc)
hMouseHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_MOUSE_LL, _
MouseHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecu tingAssembly().GetModules()(0)),
_
0)

If hMouseHook = 0 Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookProcedure = New Win32.HookProc(AddressOf
KeyboardHookProc)
hKeyboardHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_KEYBOARD_LL, _
KeyboardHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecu tingAssembly().GetModules()(0)),
_
0)

If (hKeyboardHook = 0) Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetWindowsHookEx 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***@discussions.microsoft.comwrote in message
news:94**********************************@microsof t.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.toString()) ' 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("SetWindowsHookEx 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***@discussions.microsoft.comwrote in message
news:77**********************************@microsof t.com...
If I add the lines
If (hKeyboardHook = 0) Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx 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
MouseHookProcedure = New Win32.HookProc(AddressOf
MouseHookProc)
hMouseHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_MOUSE_LL, _
MouseHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecut ingAssembly().GetModules()(0)),
_
0)

If hMouseHook = 0 Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If

If hKeyboardHook = 0 Then ' install Keyboard hook
KeyboardHookProcedure = New Win32.HookProc(AddressOf
KeyboardHookProc)
hKeyboardHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_KEYBOARD_LL, _
KeyboardHookProcedure, _

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecut ingAssembly().GetModules()(0)),
_
0)

If (hKeyboardHook = 0) Then 'SetWindowsHookEx failed
RemoveHooks()
Throw New Exception("SetWindowsHookEx failed.")
End If
End If
End Sub

even though hKeyboardHook is not 0 it executes Throw New
Exception("SetWindowsHookEx 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***@discussions.microsoft.comwrote in message
news:94**********************************@microsof t.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.toString()) ' 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
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...
15
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)...
1
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
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...
50
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
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
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...
7
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,...
10
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...
43
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.