Why do Java Developers wear glasses?

Because they are nerdy? 😎

No, because they don’t C# (see sharp). 😛

hahhaa. ok but seriously I am ramping up on some C# ASP.NET legacy project. Luckily I do not wear glasses!

So today I came across this code that has to do with events, but I have no idea once the event is raised, who’s listening, and what follows.

public delegate void MyEventHandler(String someArg);

public event MyEvent MyEvent;

private void onMyEvent() {
  if (MyEvent != null) {
    MyEventHandler("hello");
  }
}

public void doSomething() {
  // do something and trigger the event
  onMyEvent();
}

So first up, what’s a delegate? it’s a function pointer that’s safe. ok.
and a keyword “event”. That’s self-explanatory.
I see that the code did something and has raised the event. BUT who’s listening?? and what happens after the event is raised?
Visual Studio’s find call hierarchy, view reference, Alt+F12 are not helping.

and then… few searches later, I found this code in one of the aspx pages…

<uc15:UCMySection ID="UCMySection" runat="server" OnMyEvent="UCMySection _DoSomethingElse"/>

Ohhhhhhhhh. It’s the aspx page user control listening… I get it now.

I like how on the C# side, the raising event is easy to do. But not sure about the ASP.NET part. Glad we can say bye to that soon.

On a semi-related note on events and publish and subscribe, I am working on another pubsub thing for reliable message delivery. Excited to get real project experience with Kafka (previous project) and RabbitMQ (new project)!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s