1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mirthquakes
  • 343
  • 1
  • 10
  • 1
    Just subscribe to the event like you normally would. You should post what you attempted. – Security Hound Oct 03 '11 at 14:31
  • To be honest, I've actually never worked with threads in ASP.NET. My first attempts/research efforts were directed at delegate methods like 'combine' and 'adddelegate.' I was definitely going down the wrong road there. – Mirthquakes Oct 03 '11 at 14:54
  • You should research how to register an event within an ASP.NET application. One major difference is you have to worry about the session and any other unique problems to a ASP.NET page. – Security Hound Oct 03 '11 at 15:11

3 Answers3

3

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.

James
  • 80,725
  • 18
  • 167
  • 237
  • Thanks! I was able to get the event wired up using something like AddHandler service.Error, AddressOf service_Error – Mirthquakes Oct 03 '11 at 14:51
2
AddHandler service.Error, service_Error
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

I'm not sure on the VB implementation I'm afraid, but the += syntax in C# with respect to delegates, adds a method to the delegates list of methods (invocation list)

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89