Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

Basic Events Out Of The Box

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
24 Jan 2011CPOL2 min read 20.3K   91   4   6
Use simple events the same way you call a method

Introduction

When working on one on my recent projects, I needed to fire an event that contains a message. I've looked for any EventArgs that contain a string property to be used as a message container, but I didn't find anything simple. So, I decided to create a set of EventArgs that contains properties I need for almost every project and put them in a separate library, so it can be reused. Then I got the idea, why not declare the event itself and its firing method in that library to be able to fire the events from my project the same way I call a method. That's the idea behind Basic Events project, out of the box usage of events containing common properties.

The Attached Code

The attached code contains a solution with the following projects:

  • BasicEvents: The main library that contains the EventArgs classes and the Events class with events defined.
  • DMO_BLL: A business tier class to demo BasicEvents library.
  • DMO_EXE: A console application that uses the DMO_BLL class.

Using the Code

In your class that you wish to fire the event

Declare private or protected attribute of Events class.

C#
private BasicEvents.Events _events = new BasicEvents.Events();

Declare a public read only property to return the Events class instance that all the events will be fired from.

C#
public BasicEvents.Events Events
{
get
{
return _events;
}
}   

Where you want to fire one of the basic events, just call the related method:

C#
_events.FireMessageReceived(this, "Demo");

Fire events methods:

  • FireMessageReceived: fires MessageReceived event.
  • FireTimeMessageReceived: fires TimeMessageReceived event.
  • FireExceptionReceived: fires ExceptionReceived event.

In your presentation tier, or the class where you intend to handle the events fired:

Provided that you make instance of the business tier library:

C#
DMO_BLL.DMO _dmo = new DMO_BLL.DMO();

Register the event handlers:

C#
_dmo.Events.MessageReceived += 
	new BasicEvents.Events.MessageReception(Events_MessageReceived);   

Create the Events_MessageReceived method:

C#
void Events_MessageReceived(object sender, BasicEvents.MessageEventArgs e)
{
Console.WriteLine(e.Message); 
}  

Run the business method in the business tier library:

C#
_dmo.DemoMethod();  

Notes

The source code can be viewed under codeplex.com by clicking here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Link Development
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General[My vote of 1] Better solutions Pin
JP van Mackelenbergh25-Jan-11 21:06
JP van Mackelenbergh25-Jan-11 21:06 
GeneralI think a much better way would be to use Mediator pattern and WeakEvent subscription Pin
Sacha Barber25-Jan-11 0:00
Sacha Barber25-Jan-11 0:00 
GeneralMore like a tip/trick Pin
Slacker00724-Jan-11 1:30
professionalSlacker00724-Jan-11 1:30 
GeneralWhy not use Generic EventArgs instead, TData could be any type u wish to embed into event args Pin
pagal_punnu23-Jan-11 14:42
pagal_punnu23-Jan-11 14:42 
GeneralRe: Why not use Generic EventArgs instead, TData could be any type u wish to embed into event args Pin
George Nairooze24-Jan-11 23:09
George Nairooze24-Jan-11 23:09 
GeneralRe: Why not use Generic EventArgs instead, TData could be any type u wish to embed into event args Pin
KenJohnson25-Jan-11 23:36
KenJohnson25-Jan-11 23:36 
I almost always use something like that.

Ken
Ken Johnson


Latest article WPF Color Picker Construction Kit


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.