Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp)In earlier days, User has to explicitly trigger each operation in the application. But nowadays your application can trigger certain operation automatically. This is done by event oriented programming. Event oriented programming is commonly used for GUI (Graphical User Interfaces).
For example if your page has a list box and certain action has to happen when user adds a new item to the list box, then that action can be automatically triggered whenever an item is newly added to the list. This event oriented approach uses publish subscribe model wherein publisher publishes an event and when the event is fired, all its associated subscribers are notified and they can perform required action. This event oriented approach is supported in both Java and C# programming. This article will focus on achieving event oriented approach using C#. For the event oriented approach to be established, an event has to be created. You can create events using the class member modifier event. Syntax for this class member modifier is mentioned below: [Access modifier] event [Type] [Event Name]; Any of the access modifiers can be used when declaring an event. However public is the most commonly used access modifier. When an event occurs, an action has to be performed. This action is performed by the delegate. Type in the syntax above refers to the type of the delegate. Event Name is the preferable name you use for the event. Given below is an example for events which is followed by detailed explanation of the example along with the steps to be followed for achieving event oriented approach.
public class
eventDeclarationClass { public void
fireEvent() { public class
eventTriggeringClass { public static
void Main () {
public delegate
void sampleDelegate(); Create Event: You can create an event using the syntax discussed in the beginning of this article. In this example, you have created an event called sampleEvent using the following line of code: public event sampleDelegate sampleEvent; Define
Method to Fire Event: After creating an event, you have to define a method
which will fire or trigger the event. This method should be a member of
the class that declares the event. In this example, the method should
belong to the class eventDeclarationClass. Hence, method called TriggerEvent()
is defined to trigger or fire the declared event. if (sampleEvent
!= null) { For better
understanding, consider the list box example discussed at the beginning
of this article. If an item is added to the list box, the corresponding
event will have a value. If you perform other actions like deleting an
item from the list box, the event value will be null. Create New Method in Triggering Class: You can create a method in triggering class and this method will be called before event is fired. You can include print statements in this method to print appropriate statements in the console to indicate that event is going to be fired. In this example, you have created a method displayMsg inside eventTriggeringClass with a print statement. Access the Event: Event created using the class member modifier event is literally considered as a field of that class. However the triggering class which is external to the event declaring class can perform only two activities: associate a new delegate to the event or remove an associated delegate from the event. Hence += and -= are the only operators allowed to operate on an event. Example mentioned above has associated a new delegate to the event using the following line of code: sampleObj.sampleEvent += new sampleDelegate(displayMsg); Ensure that the new delegate should be of same type as that of the event declared. Fire
Event: In this example, you can fire the event by calling fireEvent method
of eventDeclarationClass as shown below:
|