.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2012

Blindly calling RaiseEvent in VB.NET

I have seen far too many examples where VB.NET code does not check to see if there are any listeners prior to raising the event. The C# examples seem to always check. For example, take a look at the MSDN code example for INotifyPropertyChanged. The two snippets below are from this site.

Raising an event in C# taken from MSDN 

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info) {

    if (PropertyChanged != null) {

        PropertyChanged(this, new PropertyChangedEventArgs(info));

    }

}

Raising an event in VB.NET taken from MSDN 

Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged
 
Private Sub NotifyPropertyChanged(ByVal info As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub

The above two snippets are fairly typical. The C# example checks to see if there are any listeners and only if there are does it raise the event. Contrast that to the VB.NET code where the RaiseEvent is called regardless of whether there are any listeners to the event.  Both code examples work fine. It would seem in VB.NET the RaiseEvent call will not throw a null exception when there are no listeners.

How do we check for listeners in the VB.NET world. The obvious would be to add If PropertyChanged IsNot Nothing Then but you'll quickly find referencing PropertyChanged in this manner will not compile. The proper way is shown in the listing below.

Checking for subscribers in VB.NET

 

    6     Public Event PropertyChanged( _

    7         ByVal sender As Object, _

    8         ByVal e As System.ComponentModel.PropertyChangedEventArgs) _

    9         Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

   10 

   11     Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)

   12         If PropertyChangedEvent IsNot Nothing Then

   13             RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))

   14         End If

   15     End Sub

The trick is to add the Event suffix to the event name, in our case PropertyChangedEvent, and check to see whether it has a value. So why don't we not see this code pattern more often. Firstly, it doesn't seem to be well documented and secondly this reference variable is not reported by intellisense.

The question is it good practise to check whether there are any listeners? In C# it is a given, otherwise, a null reference exception will be throw. In VB.NET no exception will be thrown and the call to RaiseEvents will not throw an exception when there are no listeners. But which is faster? I wrote a simple application to check the amount of time it takes to blinding call RaiseEvent versus the check for the existence of the listener. Checking the listeners prior to calling RaiseEvent is about 1.75 faster than just calling RaiseEvent blindly. It should be noted that even though the check is 1.75 times faster, the actual amount of time to call RaiseEvent is extremely small. A call to RaiseEvent with no listeners takes approximately 0.000000008 seconds.

In the end, checking for listeners, in VB.NET, will in all likelihood not impact the performance of the code.

Guess the movie

Why am I such a misfit? I am not just a nitwit. You can't fire me, I quit. Seems I don't fit in.

Currently rated 4.0 by 4 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VB.NET
Posted by CynotWhyNot on Tuesday, December 09, 2008 6:38 PM
Permalink | Comments (39) | Post RSSRSS comment feed

Related posts

Comments

designer handbag US

Thursday, May 27, 2010 2:34 PM

designer handbag

I find your blog in google. And I will be back next time, thanks.

discount prada bags US

Monday, May 31, 2010 5:28 PM

discount prada bags

What a great info, thank you for sharing. this will help me so much in my learning.

discount louis vuitton US

Thursday, June 03, 2010 7:13 PM

discount louis vuitton

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

mattress toppers us

Tuesday, September 21, 2010 5:12 PM

mattress toppers

yhyezwqhrsudcbitvrfd, http://honorar.si, MjuyonU.

delo zaposlitev us

Sunday, October 03, 2010 5:30 PM

delo zaposlitev

wspzbuquaaokqmb, <a href="http://kathyvanzeeland.org">kathy van zeeland</a>, tDEVbSixQQNHjogUMXDO.

stearns and foster us

Monday, October 04, 2010 5:00 PM

stearns and foster

birwcilovtmjfot, <a href="http://mattresstopperbuy.com">mattress topper</a>, YfCiztNDxAbWSQYGZayg.

william rast us

Tuesday, October 05, 2010 10:52 AM

william rast

tmlknssmjiycdep, <a href="http://honorar.si">zaposlitev</a>, RJicjYDfQiCpHxCisKLl.

pantene pro v us

Wednesday, October 06, 2010 6:38 PM

pantene pro v

tqplvambfkdkqsxumtsc, http://kathyvanzeeland.org kathy van zeeland , UVRzrthSezdhbVtRYu. kigpcgmuczvtnskonfwu, http://spaceheaterbuy.com space heater , vvzhOuVXpRtmCtHdsl.

william rast us

Wednesday, October 06, 2010 7:55 PM

william rast

avjjfyrjzmejilxjsack, http://donnakaranshoes.org donna karan shoes , rdqjoAPTOiMkWmBptz. svrhpdlhtrfvmtkazjjs, http://1800mattress.org 1800 mattress , XNYzpziMPPRmurjSaG.

garnier nutrisse us

Thursday, October 07, 2010 4:22 PM

garnier nutrisse

kbjsjqsbtfkrbraxzrnc, http://1800mattress.org 1800 mattress , wTaWIeXxlhRdTURBdt. gclbxqlyohribwseldoq, http://spaceheaterbuy.com space heater , SvSeHAzxsfWHwqEoPT.

maximuscle cyclone us

Monday, October 11, 2010 12:55 PM

maximuscle cyclone

csrmuqjtklvobjkadg, http://www.bodyactive-onlinestore.co.uk , cXiorsTdDiLDLptnXUwJ.

biorhythm after glow us

Thursday, October 14, 2010 8:40 AM

biorhythm after glow

ghqgxgpuggbqhsyzmr, http://biorhythm-androbolix.co.uk , qinGRxmApGRgCRDEevqX.

phd synergy vs cyclone us

Saturday, October 16, 2010 5:33 PM

phd synergy vs cyclone

aepjgvermbxdhnozlr, http://www.affordablesupplementstore.co.uk , LLcvZiHxGIFHHxjRLskv.

Compact refrigerator us

Wednesday, November 03, 2010 5:38 AM

Compact refrigerator

Thanks for posting this. i really had good time reading this.

Compact refrigerator us

Wednesday, November 03, 2010 10:18 PM

Compact refrigerator

Its a pity you dont have a donate button, i would donate some =)

Cross cutting shredder us

Thursday, November 04, 2010 5:54 AM

Cross cutting shredder

Its a pity you dont have a donate button, i would donate some =)

Sephora collection colorful artist palette warm colorful artist palette warm

I loved this post

Ed hardy love luck gift 4pcs gift set for women by christian audigier

I liked this article

Cross cutting shredder us

Friday, November 05, 2010 6:44 AM

Cross cutting shredder

This site is cool. i visit here evaryday.

Adorama video motorized pan head us

Friday, November 05, 2010 9:37 AM

Adorama video motorized pan head

I loved this post

Cross cutting shredder us

Sunday, November 07, 2010 5:57 AM

Cross cutting shredder

Great site design!!!! Whattemplate did you use?

Day of the dragon us

Sunday, November 07, 2010 1:28 PM

Day of the dragon

Its a pity you dont have a donate button, i would donate some =)

Koji eye talk double eyelid maker us

Sunday, November 07, 2010 4:26 PM

Koji eye talk double eyelid maker

Thanks for posting this. i really had good time reading this.

Male kitten bullying his sister us

Sunday, November 07, 2010 7:52 PM

Male kitten bullying his sister

Where is your rss? I cant find it

HD Vegetation HD Wallpaper us

Sunday, November 07, 2010 10:49 PM

HD Vegetation HD Wallpaper

This site is great. i visit here evaryday.

Facebook statuses us

Monday, November 08, 2010 1:33 AM

Facebook statuses

Thanks for posting this. i really had good time reading this.

Emo us

Monday, November 08, 2010 4:26 AM

Emo

This site is great. i visit here evaryday.

Compact refrigerator us

Monday, November 08, 2010 7:14 AM

Compact refrigerator

Hey check out my blog too. I hope i have some cool stuff too

Cross cutting shredder us

Monday, November 08, 2010 10:00 AM

Cross cutting shredder

Its a pity you dont have a donate button, i would donate some =)

Luigi smith us

Saturday, November 27, 2010 2:16 PM

Luigi smith

Chrismas season, Look for top pork recipes, visit http://www.topporkrecipes.com.

Phone cards au

Tuesday, November 30, 2010 8:51 AM

Phone cards

Presented with effective manner and a good case study post will surely the top noch one. i expect this kinda post in future also to merely reduce the prices adjoin. thanks for the post and great analysis

Facebook statuses us

Monday, December 13, 2010 9:41 AM

Facebook statuses

I loved this article

Emo us

Monday, December 13, 2010 2:57 PM

Emo

Hey check out my blog too. I hope i have some cool stuff too

Compact refrigerator us

Tuesday, December 14, 2010 2:17 AM

Compact refrigerator

Thanks for posting this. i really enjoyed reading this.

Cross cutting shredder us

Tuesday, December 14, 2010 11:13 AM

Cross cutting shredder

Great site design!!!! Whattemplate did you use?

Manfrotto 234rc monopod head quick release replaces 3229

I liked this article

Michael jackson tribute bucharest 5 minutes lesson dance steps

Thanks for posting this. i really had good time reading this.

Jacob black facebook statuses us

Sunday, December 26, 2010 7:40 AM

Jacob black facebook statuses

Hey check out my blog too. I hope i have some interesting stuff too