NeuralJack Posted October 3, 2007 Posted October 3, 2007 I'm trying to convert a C# class into VB.net and have come up with a problem. The C# code is: public event MouseEventHandler OnMouseActivity; public void MySub() { if (OnMouseActivity != null) { } } And I would have guessed the VB.net equivalent is: Public Event OnMouseActivity As MouseEventHandler Public Sub MySub() If OnMouseActivity isnot Nothing Then End If End Sub But that does not work. The error message is "... is an event and can not be called directly." It's obviously some difference in the syntax in the way events are handled. I know that in C# you can call an event much like a function, but in VB.net you must use RaiseEvent. But upon further research I can not find a way to check for an event's instantiation in a conditional statement. Thanks for any help Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Administrators PlausiblyDamp Posted October 3, 2007 Administrators Posted October 3, 2007 Public Sub MySub() If Not OnMouseActivity Is Nothing Then End If End Sub should do it. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NeuralJack Posted October 3, 2007 Author Posted October 3, 2007 Thanks for the quick reply ,but that doesn't seem to work. Here's a quick way to test it with this cut/paste class Public Class test Public Event OnMouseActivity As MouseEventHandler Public Sub MySub() If Not OnMouseActivity Is Nothing Then End If End Sub End Class Its the same error as before. I hope I'm not missing something. The C# class simply uses OnMouseActivity != null and that works fine Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Administrators PlausiblyDamp Posted October 3, 2007 Administrators Posted October 3, 2007 Public Class test Public OnMouseActivity As MouseEventHandler Public Sub MySub() If Not OnMouseActivity Is Nothing Then OnMouseActivity() End If End Sub End Class should do it ;) - Events are slightly different under vb Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NeuralJack Posted October 3, 2007 Author Posted October 3, 2007 Thanks! :D Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Leaders snarfblam Posted October 3, 2007 Leaders Posted October 3, 2007 Keep in mind that with that last code listing you won't be able to use event syntax (AddHandler, RemoveHandler). Also, with events in VB, you don't need to check them for null (Nothing) before raising them--VB does that for you. [Color=Green]'This code will also work [/Color][Color=Blue]Public Class[/Color] Test [Color=Blue]Public Event [/Color]OnMouseActivity [Color=Blue]As[/Color] MouseEventHandler [Color=Blue]Public Sub[/Color] MySub() [Color=Green]'You won't get a NullReferenceException here just because there is no handler [/Color] [Color=Blue]RaiseEvent [/Color]OnMouseActivity([Color=Blue]Me[/color], [Color=Blue]New [/Color]MouseEventHandler(MouseButtons.Left, 0, 0, 0, 0)) [Color=Blue] End Sub End Class[/Color] [Color=Green]'Now we can also use AddHandler/RemoveHandler[/Color][/Code] Quote [sIGPIC]e[/sIGPIC]
NeuralJack Posted October 3, 2007 Author Posted October 3, 2007 Thanks marble. I found out you couldnt use AddHandler, RemoveHandler so the way I did it was : Test.OnMouseActivity = New MouseEventHandler(AddressOf Me.MouseMoved) But even though there are no detected errors I'm not at all sure if this is the way to go about it. I'm translating a C# class into VB.net that sets up a low level windows hook for the keyboard and mouse. It works great in C#, but still cant get it going in VB.net. My guess is that the problem is in using an API or two (like SetWindowsHookEx) .. even though the declarations are identical to the original C# classes declaration. I am wondering if there is a good way to use both C# and VB.net code in a project besides creating a .dll in C# and importing it into VB.net? These classes are big or I'd paste them. If someone has the desire to see what I might be doing wrong send me a PM and I"ll email them to you or PM them to you. Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
mskeel Posted October 5, 2007 Posted October 5, 2007 I am wondering if there is a good way to use both C# and VB.net code in a project besides creating a .dll in C# and importing it into VB.net?There are, but it's messy. DLL's are pretty simple and get the job done. I think there are even tools out there that combine DLL's and executables into a single executable if that is your need. Quote
Leaders snarfblam Posted October 15, 2007 Leaders Posted October 15, 2007 I think there are even tools out there that combine DLL's and executables into a single executable if that is your need. IL Merge. Or ILMerge. Or IlMerge. Not sure about the casing and the spacing, but anyways, its actually made by Microsoft. You can download it from Microsoft (though it might be one of those downloads where you need to have a Windows authentication plug-in installed and have no prior convictions for thought-crimes). Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.