C# ->VB.Net 中的 RaiseEvent [英] C# -> RaiseEvent in VB.Net

查看:26
本文介绍了C# ->VB.Net 中的 RaiseEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB.Net 中使用 TweetInvi 很开心,不幸的是我在将此代码转换为 VB.Net 时遇到了问题.我仍然是初学者,我试图获取有关 RaiseEvent 的一些信息,但我无法做到.这是代码.我想在按钮事件中运行它:

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();

谢谢.

推荐答案

事实上,您不是要提出一个事件,而是订阅 一个.不幸的是,将该代码转换为 VB.NET 时出现的 IntelliSense 错误有点误导.

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.

就事件而言,C# 的 += 运算符等于 Delegate.Combine() 将另一个委托添加到事件的订阅者列表(事件处理程序列表).Delegate 只是一个持有另一个方法指针的类.委托用于提供一种通过代码传递和调用方法的简单方法.

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.

引用 文档:

+= 运算符还用于指定响应事件时将调用的方法;此类方法称为事件处理程序.在此上下文中使用 += 运算符称为订阅事件.

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.

要订阅 VB.NET 中的事件,您必须使用 AddHandler 语句,其语法为:

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>

因此:

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

- 末尾的下划线 (_) 只是告诉编译器继续下一行的一种方式.在较新版本的 VB.NET 中,这不是必需的,但有些人仍在使用 VS 2008 及更低版本...我也喜欢将其放在那里,以便更清楚哪些语句可以组合在一起,哪些不能组合在一起.

- 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.

这篇关于C# ->VB.Net 中的 RaiseEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆