C# to VB.Net Conversion Prob

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
I'm trying to convert a C# class into VB.net and have come up with a problem.

The C# code is:
Code:
public event MouseEventHandler OnMouseActivity;

public void MySub()
{
 	if (OnMouseActivity != null)
	{
	}
}

And I would have guessed the VB.net equivalent is:
Code:
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
 
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

Code:
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
 
Visual Basic:
    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
 
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.
Code:
[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]
 
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.
 
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.
 
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).
 
Back
Top