C#

Using C# Events (for beginners)

There are plenty of books on C# and thousands of topics on C# events/delegates. But nothing can beat Head First C# book by O’Reilly. Its so Fun reading the chapters and they way they explained it. I read the chapter about events and delegates and immediately created a small sample application showing the usage of events.

In this example I’ll build a simple stock quote provider and applications that subscribe to this provider to get stock quote.

The first step is to build the stock quote provider class. The StockQuoteProvider class will have an event “UpdateQuote” and a method “OnUpdateQuote” which will raise the event “UpdateQuote”. The “OnUpdateQuote” method will have a parameter “StockQuoteArgs” parameter. The purpose of “StockQuoteArgs” event argment is to send any arguments to the receiver. Basically we will be sending the Stock Code and the current price of the stock to the receiver.

 

 

 

 

 

 

Create a subscriber method as shown below.


Subscriber

 

 

 

 

 

 

Implement a simple windows application to test the StockQuoteProvider and the Subscribers.

 

 

 

 

 

Now run the application to test the Stock Quote Provider application

Sample App

 

 

 

 

Download the source code from here

https://cid-d567c8f9d6618608.skydrive.live.com/redir.aspx?resid=D567C8F9D6618608!117&authkey=tobZQeSUrkU%24

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s