C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Event Handling in C#: Custom event Handlers in .NET


Posted Date: 31 Jan 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: VasanthRaj       Member Level: Gold
Rating:     Points: 10




To illustrate event handling we will make a news service that notifies its subscribers whenever an event (new news) occurs. Subscribers can unsubscribe and subscribe at will.


The delegate:
The delegate lets the subscriber know what type method can be notified. Essentially, the news service says “In case of an event occurring, I’ll notify any registered method so long as it has the same signature as the delegate” and the subscriber says, “Okay, register this method.” The method could be in any class.



We will use the following classes:


NewsService: Notifies the subscribers when events (new news) occur.
Subscriber: Instances subscribe to the news service, indicating what method should be notified.
News: Instances of this class are the parameter passed from the news service to the registered method. Contrary to what most people seem to think, it does not have to inherit from EventAgs. Any parameters can be passed.






using System;

// The delegate indicates what type methods can be notified of an event.
// The method(s) must have the same signature as the delegate.
public delegate void NewsDelegate(News news);

public class News
{
public string news;
public News(string news) {this.news=news;}
}

// This class notifies subscribers when the specified event occurs.
// In this case the event is that newNews() is called.
public class NewsService
{
// Must have the format: public event delegateName anyName
public event NewsDelegate NewsEventSubscribers;

public void newNews(string news)
{
Console.WriteLine("An event! New news.");
// first check if there are subscribers to this event
if (NewsEventSubscribers !=null)
{
// This will send an instance of News to each subscriber of local news
NewsEventSubscribers(new News(news));
}
}
public NewsService(){}
}

public class Subscriber
{
public string name;
NewsDelegate delNews;

public void SubscribeNewsService(NewsService ns)
{
// Create a new NewsDelegate. Its parameter is the method to notify
// when the event occurs. This method may also be in another class,
// in which case the parameter is InstanceOfSomeOtherClass.method
delNews=new NewsDelegate(WriteNews);
// Register the delegate with the NewsService
ns.NewsEventSubscribers +=delNews;
Console.WriteLine(name + " subscribed to news.");
}

public void UnsubscribeNewsService(NewsService ns)
{
// unsubscribing
ns.NewsEventSubscribers -=delNews;
Console.WriteLine(name + " unsubscribed to news.");
}

// This method must have the same signature as
// the delegate NewsDelegate declared above.
public void WriteNews(News e)
{
Console.WriteLine(name + " received news: " + e.news);
}

public Subscriber(string name) {this.name=name;}
}

class MakeNews
{
static void Main(string[] args)
{
NewsService ns=new NewsService();
Subscriber julie=new Subscriber("Julie");
julie.SubscribeNewsService(ns);
Subscriber matthew=new Subscriber("Matthew");
matthew.SubscribeNewsService(ns);
ns.newNews("More money for schools.");
julie.UnsubscribeNewsService(ns);
ns.newNews("New mayor.");
}
}









Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Untill keypressed / conio.h
Previous Resource: Differences between constant and read only variable
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use