-3

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.

dwarfmine
  • 15
  • 3
  • You could give this a try -> http://converter.telerik.com/ – Krazy Dev Aug 29 '17 at 19:19
  • Number two under **Related**: [How can I RaiseEvent in VB.net](https://stackoverflow.com/questions/7805496/how-can-i-raiseevent-in-vb-net?rq=1) ?? Its not at all clear what you (arent) asking. Please read [ask] and take the [tour]...but at least *you* are having fun – Ňɏssa Pøngjǣrdenlarp Aug 29 '17 at 19:20
  • Krazy, I tried it and it doesn't help this time. Plutonix like I said I googled, I read StackOverflow about RaiseEvent but I still don't understand. – dwarfmine Aug 29 '17 at 19:23
  • 1
    `Krazy, I tried it and it doesn't help this time`, what doesn't help? What is the error? – Trevor Aug 29 '17 at 19:36
  • 2
    Even *krazier* is joining a QA site known for being picky about the questions asked and how they are asked, and not reading the site guidelines first such as [ask] and the [tour] – Ňɏssa Pøngjǣrdenlarp Aug 29 '17 at 19:52

1 Answers1

-1

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.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Thank you! Seems like it works. – dwarfmine Aug 29 '17 at 19:55
  • 2
    Good answer, but ***you*** should know by now answering these questions are off topic. If the OP ***would post what doesn't work***, then it would be appropriate to help... Almost all of your answer has already been explained here on SO many times, why do we need more? Plutonix posted a link explaining this, I asked a question, but no answer from OP... – Trevor Aug 29 '17 at 19:56
  • @dwarfmine : Glad I could help, and welcome to Stack Overflow! Please mark my answer as "accepted" by pressing the tick/check mark on its left if it solved your problem. – Visual Vincent Aug 29 '17 at 19:56
  • I don't want to start posting links, should I? Just on the ***related*** area, I see about 6 that are useful :) How about https://stackoverflow.com/questions/30870460/raiseevent-issue-while-converting-code-c-sharp-to-vb-net?rq=1 that one or https://stackoverflow.com/questions/34277855/c-sharp-to-vb-net-conversion-raiseevent-troubles?rq=1 this one? That is just two under **related** – Trevor Aug 29 '17 at 20:00
  • I am talking about your answer for this question :) – Trevor Aug 29 '17 at 20:03
  • He helped me, don't attack him :) I am sorry, I made wrong question. – dwarfmine Aug 29 '17 at 20:06
  • @Codexer : Oh, haha. Misread your comment. I thought _"almost all of your answer"_ was a typo where you actually meant _"all of your answer-**S**"_. -- Well I understand your point. And you're right - there are already many that are answered, however I thought I'd make an actual explanation in this one since people often are confused by the error they receive when converting (also, in my defense I am writing from my phone, so I cannot see the "Related" section on the mobile site ;). – Visual Vincent Aug 29 '17 at 20:07
  • 2
    @VisualVincent no problem. What happens is people think just posting code and saying it doesn't work we will feed them whatever they want. SO is not about that and I believe that is why many come along and vote to close these questions because lack of information and or research into the problem they are reporting. If people start answering these, in which they have, they get the impression it's ***ok***... – Trevor Aug 29 '17 at 20:12
  • @Codexer : You are right, of course. I am usually also one to vote-for-close poorly asked questions, though sometimes (like this one) I do the mistake of letting them get through (mostly with questions that I find rather simple but yet not _**too**_ obvious). I need better self-control ;) – Visual Vincent Aug 29 '17 at 20:23
  • @Codexer : While I know there already is plenty of information about it, I was actually planning on writing some good to remember "dos and don'ts" for Stack Overflow on the future update of my website (it is in a horrible state right now, so don't look at it ;). Mostly it'd mention those **really** important things that you should/shouldn't do when asking a question. – Visual Vincent Aug 29 '17 at 20:31