I am having fun with TweetInvi in VB.Net, unfornately I have issue with converting this code to VB.Net. I am still beginner and I was trying to get some information about RaiseEvent, but I couldn't do it. Here is code. I want to run this in button event:
var stream = Stream.CreateFilteredStream();
stream.AddTrack("tweetinvi");
stream.MatchingTweetReceived += (sender, args) =>
{
Console.WriteLine("A tweet containing 'tweetinvi' has been found; the tweet is '" + args.Tweet + "'");
};
stream.StartStreamMatchingAllConditions();
Thanks.
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.