Events in CSharp
Events in CSharp
This is a continuation to my previous post: Delegates : A Step Ahead Series
In C# events are based on delegates, with the originator defining one or more call back functions. A call back function is a function in which one piece of code defines another implements, in other words, one piece of code says, “If you implement a function which looks like this, I can call it". A class that wants to sue events defines callback functions or delegates, and the listening object then implements them.
An event is a member that enables an object or class to provide notifications.
As compared to delegates events works with source and listener methodology. So listeners who are interested in receiving some events they subscribe to the source. Once this subscription is done the source raises events to its entire listener when needed. One source can have multiple listeners.
Note: Events have no return type.
/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : msdotnetheaven */
// File name : events.cs
using System;
namespace CSharp.AStepAhead.events
{
class Button
{
public event EventHandler Click;
public void Reset()
{
Click = null;
}
}
public class impButton
{
Button Button1 = new Button();
public impButton()
{
Button1.Click += new EventHandler(Button1_Click);
}
public void Connect()
{
Console.WriteLine("Attaching the event handler");
Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object s, EventArgs e)
{
Console.WriteLine("Button1 clicked!");
}
public void Disconnect()
{
Console.WriteLine("Removng the event handler");
Button1.Click -= new EventHandler(Button1_Click);
}
static void Main()
{
impButton objButton = new impButton();
objButton.Connect();
Console.ReadLine();
objButton.Disconnect();
Console.ReadLine();
}
}
}
Difference between Delegates and Events :
There are following two differences:
1. Events use delegates.
2. Delegates are function pointers they can move across any client.
namespace Delegate__event
{
// This is Two Delegate
public delegate void BPPetrolpump();
public delegate void HPPetrolpump();
// This is class Price
class Price
{
double _HP_Petrol;
double _HP_Diesal;
double _HP_CNG;
double _BP_Petrol;
double _BP_Diesal;
double _BP_CNG;
// This is two Event
event BPPetrolpump Bharat_petrolium;
event HPPetrolpump Hindustan_Petrolium;
// This is Constractor
public Price()
{
_HP_Petrol = 53.34;
_HP_Diesal = 48.45;
_HP_CNG = 39.21;
_BP_Petrol = 52.56;
_BP_Diesal = 47.90;
_BP_CNG = 38.52;
}
// This is first Method
void Price_of_Hindustan_pet()
{
Console.WriteLine("--------------------------------");
Console.WriteLine("Price of Hindustan petrolium is:");
Console.WriteLine("--------------------------------");
Console.WriteLine("HP Price of PETROL is:\t" + _HP_Petrol);
Console.WriteLine("HP Price of DIESAL is:\t" + _HP_Diesal);
Console.WriteLine("HP Price of CNG is:\t" + _HP_CNG);
Console.WriteLine("--------------------------------");
}
// This is Second Method
void Price_of_Bharat_pet()
{
Console.WriteLine("\n--------------------------------");
Console.WriteLine("Price of Bharat petrolium is:");
Console.WriteLine("--------------------------------");
Console.WriteLine("\nBP Price of PETROL is:\t" + _BP_Petrol);
Console.WriteLine("BP Price of DIESAL is:\t" + _BP_Diesal);
Console.WriteLine("BP Price of CNG is:\t" + _BP_CNG);
Console.WriteLine("--------------------------------");
}
static void Main(string[] args)
{
Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Console.WriteLine(" Price of Petrolium");
Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
Price p1 = new Price();
p1.Bharat_petrolium+=new BPPetrolpump(p1.Price_of_Bharat_pet);
p1.Bharat_petrolium();
p1.Hindustan_Petrolium += new HPPetrolpump(p1.Price_of_Hindustan_pet);
p1.Hindustan_Petrolium();
}
}
}
The return type of the event is alwys dalegate