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 delegate void sampleDelegate();

public class eventDeclarationClass {
public event sampleDelegate sampleEvent;

public void fireEvent() {
if (sampleEvent != null) {
sampleEvent();
}
}
}

public class eventTriggeringClass {
private static void displayMsg() {
Console.WriteLine("Event is Fired");
}

public static void Main () {
eventDeclarationClass sampleObj = new eventDeclarationClass();
sampleObj.sampleEvent += new sampleDelegate(displayMsg);
sampleObj.fireEvent();
}
}


For establishing event oriented programming, you have to perform the following steps:
• Create Delegate: To create an event you need a delegate. .NET provides pre-defined delegates. You can choose one from that if it suits your need or you can create your own delegate. You have created a new delegate in the example above using the following line of code:

public delegate void sampleDelegate();

• Create Class or Interface for Declaring and Defining Event: You can declare events either inside a class or inside an interface. In this example, you have created a class called eventDeclarationClass inside which you have declared and defined the event called sampleEvent.

• 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.

• Write Code to Invoke Event: When the event has still not occurred, sampleEvent will be null. If the event has occurred then it will have a value. Hence write appropriate code for invoking event as shown below:

if (sampleEvent != null) {
sampleEvent();
}

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 Class to Trigger Event: Now that you have declared and defined event, you have to create a new class which will trigger the event. Example discussed above has a class called eventTriggeringClass for triggering sampleEvent.

• Create an Instance of Class that has Event Declaration: In this example, you have to create an object of eventDeclarationClass using following line of code:


eventDeclarationClass sampleObj = new eventDeclarationClass();

• 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:
sampleObj.fireEvent();

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |

 


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.