Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

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

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
  • Leaders
Posted

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]

[sIGPIC]e[/sIGPIC]
Posted

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.

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted
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.
  • 2 weeks later...
  • Leaders
Posted
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).

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...