如何在VB.NET中使用C#编写的以下事件/ delgates? [英] How can I use the following events/delgates, written in C#, in VB.NET?

查看:169
本文介绍了如何在VB.NET中使用C#编写的以下事件/ delgates?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ASP.NET网络应用程序中使用JdSoft的APNS-Sharp库。该库用C#编写,并广泛使用代理函数和事件进行线程化。我的应用程序是用VB.NET编写的,有关如何翻译以下示例代码(C#),我有点困惑:

  .... 
//绑定事件
service.Error + = new FeedbackService.OnError(service_Error);
....
}

static void service_Error(object sender,Exception ex)
{
Console.WriteLine(...);
}

以下是FeedbackService类的相关成员:

  public delegate void OnError(object sender,Exception ex); 
public event OnError Error;

基本上,我试图找出如何将一个函数(如service_Error)附加到一个事件(如错误)在VB.NET。我不清楚这个上下文中+ =语法是什么意思,VisualStudio表示,由于某些原因,我的VB.NET代码无法直接访问'Error'事件。



谢谢!

解决方案

= 操作符基本上订阅 Error 调用列表的 FeedbackService.OnError 函数。所以当错误事件发生时,调用 OnError 方法。



要将上述代码翻译成VB.NET,它将如下所示:

  // define delelgate /事件
公共委托Sub OnError(发件人作为对象,ex As Exception)
公共事件OnError错误

//附加方法到事件
AddHandler service.Error,service_Error

请参阅如何:提升和使用事件在VB.NET中的一些示例。


I'm using JdSoft's APNS-Sharp library in my ASP.NET web app. The library is written in C#, and makes extensive use of Delegate Functions and Events for threading purposes. My application is written in VB.NET, and I'm a little confused on about how to translate the following sample code (C#):

....
//Wireup the events
service.Error += new FeedbackService.OnError(service_Error);
....
}

static void service_Error(object sender, Exception ex)
{
Console.WriteLine(...);
}

Here are the relevant members of the FeedbackService class:

public delegate void OnError(object sender, Exception ex);
public event OnError Error;

Basically, I'm trying to figure out how to attach a function (like service_Error) to an event (like Error) in VB.NET. I'm unclear on what the += syntax means in this context, and VisualStudio says that the 'Error' event cannot be accessed directly by my VB.NET code for some reason.

Thanks!

解决方案

The += operator is basically subscribing the FeedbackService.OnError function to the Error invocation list. So when the Error event is raised, the OnError method is invoked.

To translate the above code to VB.NET, it would look something like:

// define delelgate/event
Public Delegate Sub OnError(sender As Object, ex As Exception)
Public Event OnError Error

// attach method to event
AddHandler service.Error, service_Error

See How to: Raise and Consume Events for some examples in VB.NET.

这篇关于如何在VB.NET中使用C#编写的以下事件/ delgates?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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