Sunday, 1 November 2015

Why there is no Nothing check for Events and how we can check the Nothing for Events in VB.Net

In this post we are going to see why there is no nothing check code is not present for the events in the VB.Net. In Generally while we code in c#, we can see the code like check the event not to be null, then we will fire it. But in VB.Net
we can't find a code like this.

Normally how we raise a event in VB.Net


Public Delegate Sub 
CheckBoxHeaderClickHandler(sender As Object, e As CheckBoxHeaderCellEventArgs)


Declare the Event
***********************
 Public Event OnHeaderClick As CheckBoxHeaderClickHandler

Calling the Event
***********************
 RaiseEvent OnHeaderClick(Me, CHECK_ARGS)

In the Above code we can see that before the RaiseEvent Directly call the Events instead of checking the null, this is why means,By default RaiseEvent will check for Nothing before fire the Event, so we don't need to worry about this, but if there is a need to check whether the event is hooked or not , then how we can find this ? 

RaiseEvent doesn't have any return type to find whether Event is there or not, then how we can find this whether Event is hooked, By Default a variable is created with EventName + 'Event' , we can use that variable to check for Nothing

For Example:   OnHeaderClickEvent 


Public Delegate Sub 
CheckBoxHeaderClickHandler(sender As Object, e As CheckBoxHeaderCellEventArgs)


Declare the Event
***********************
 Public Event OnHeaderClick As CheckBoxHeaderClickHandler



Calling the Event
***********************
            If OnHeaderClickEvent IsNot Nothing Then                
                RaiseEvent OnHeaderClick(Me, CHECK_ARGS)
            End If


From this post you can see why there  is no Nothing check for Events and how we can check the Nothing in Events in Vb.Net

No comments:

Post a Comment