[Solved] C# -> RaiseEvent in VB.Net [closed]


As a matter of fact you’re not trying to raise an event, but subscribe to one. The IntelliSense error that you get when converting that code to VB.NET is unfortunately a bit misleading.

In terms of events, C#’s += operator is equal to Delegate.Combine() which adds another delegate to an event’s subscribers list (list of event handlers). A Delegate is simply a class holding the pointer of another method. Delegates are used to provide an easy way of passing around and invoking methods through code.

Quoting the documentation:

The += operator is also used to specify a method that will be called in response to an event; such methods are called event handlers. The use of the += operator in this context is referred to as subscribing to an event.

To subscribe to events in VB.NET you gotta use the AddHandler statement, which’s syntax is:

AddHandler <event to subscribe to>, <method to be invoked when the event occurs>

Thus:

AddHandler stream.MatchingTweetReceived, _
    Sub(sender As Object, args As EventArgs)
        Console.WriteLine("A tweet containing 'tweetinvi' has been found; the tweet is '" & args.Tweet & "'")
    End Sub

– The underscore (_) on the end is just a way of telling the compiler to continue on the next line. In the newer versions of VB.NET this isn’t necessary, but some people still use VS 2008 and below… I also like to have it there to make it more clear which statements go together and which not.

10

solved C# -> RaiseEvent in VB.Net [closed]