problem with conditional statements in vb.net IDE

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

Robin S.
 
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.GetExecutingAssembly().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.GetExecutingAssembly().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
 
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.
 
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.


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

Robin S.
------------------------------
 
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.
 
Back
Top