Muckers Mate
Muckers Mate

Reputation: 429

VB.Net DLL Event, C# Handler

I have a VB.NET dll (written by someone else) and I require that it publishes and raises an event that will (typically) be handled by C#.

If the DLL were C# then this would be simple,

(client)
myDLL.OnClick += doClick

(Dll)
if (OnClick != null) OnClick(....

i.e. 2 lines of code

but perhaps it's my frustration at VB.NET, but I cannot find a simple example of how to do this.

Can someone please show/explain how to define and raise an event in VB.NET, that can be handled within C#?

Thanks

MM

Upvotes: 0

Views: 762

Answers (2)

Sam Axe
Sam Axe

Reputation: 33738

VB Uses the Event keyword.

Public Event SomethingChanged (byval sender as object, e as eventargs)

Subscribe to it in C# as you would any other event.

Upvotes: 0

Justin
Justin

Reputation: 86789

I've just tested this and its exactly the same as it normally is - raise the event in VB.Net (as you normally would)

Public Class Class1
    Public Event TestEvent()

    Public Sub RaiseTestEvent()
        RaiseEvent TestEvent()
    End Sub
End Class

Then reference the VB.Net assembly and consume the event in C# (again, as you normally would)

var class1 = new ClassLibrary1.Class1();
class1.TestEvent += () => Console.WriteLine("Test Event");
class1.RaiseTestEvent();

See Raising Events and Responding to Events

Upvotes: 2

Related Questions