What is an event? Event means something we do like postingthis article, reading this article etc. Everything we do, is an event. In orderto illustrate event driven programming, I would like to tell a story and showhow I can put this real life scenario using event. So start thinking OOPs.
During my schooling, I was very worried about my semesterresult time. Why? I was not a good student and I used to play whole day whichgenerally reflected in my semester result. My parent scolded me lot for theseactivities. Anyway I informed them about my result.
In the above problem, we can easily understand two objectsare mainly involved. One is Parent and Me (Student). So when my score will be published,I need to inform my parent. This is achieved by following code.
First define one class Parent with a name and a method.
class Parent
{
public String Name{ get; set; }
public void NotifyMe(int pGPAScore)
{
Console.WriteLine(String.Format(“{0} notified about the GPA {1}”, Name,pGPAScore.ToString()));
}
}
Declare one delegate
public delegate void NotifyGpaDelegate(int GPAScore);
why we use this delegate. Because we need to call Parent’s NotifyMe method. So the delegatesignature would be identical with the NotifyMemethod’s signature.
Declare Student class with name and GPA score
class Student
{
public event NotifyGpaDelegate NotifyToParent;
public String Name{ get; set; }
public int GPAScore {get; set; }
public voidRecordGPAScore()
{
if (NotifyToParent != null)
NotifyToParent(GPAScore);
}
}
Here we need to understand that Student calls the method NotifyMe of Parent class. So we need todeclare a delegate which will point to the NotifyMemethod.
Here someone can argue that why don’t I instantiate oneParent object and call the NotifyMemethod withinRecordGPAScore method.I would say, this can create a dependency between Student class and ParentClass. Suppose in future, Student class needs to notify another class likeTeacher class, then you don’t need to instantiate Teacher class within Student.You just need to write the eventhandler for it.
Think about the Button class and TextBox class. Button class hasOnClick event. Now if we write any specific code within Button class, would itbe reusable? So better define delegate, then an event of that delegate type andtell to developers that the method you want to call, should match thisdelegate’s signature. The same thing we are doing here. We defined one delegateNotifyGPADelegate, then an event NotifyToParent of that delegate type andask developers to subscribe to this event.
Here how we wire up these two objects
StudentoStudent = new Student();
oStudent.Name = “James”;
oStudent.GPAScore = 80;
Parent oParent = newParent();
oParent.Name = “Daddy Cool”;
oStudent.NotifyToParent += new NotifyGpaDelegate(oParent.NotifyMe);
oStudent.RecordGPAScore();
We are instantiating one Student class and Parent class.Then we subscribe the NotifyToParentevent with the method NotifyMe ofParent class. Atlast we call the method to RecordGPAScore,which in turn call the NotifyMeofParent class.
Sample code looks like
class Program
{
static void Main(string[] args)
{
Student oStudent = newStudent();
oStudent.Name = “James”;
oStudent.GPAScore = 80;
Parent oParent = newParent();
oParent.Name = “Daddy Cool”;
oStudent.NotifyToParent += new NotifyGpaDelegate(oParent.NotifyMe);
oStudent.RecordGPAScore();
}
}
public delegate void NotifyGpaDelegate(int GPAScore);
class Student
{
public event NotifyGpaDelegate NotifyToParent;
public String Name{ get; set; }
public int GPAScore {get; set; }
public voidRecordGPAScore()
{
if (NotifyToParent != null)
NotifyToParent(GPAScore);
}
}
class Parent
{
public String Name{ get; set; }
public void NotifyMe(int pGPAScore)
{
Console.WriteLine(String.Format(“{0} notified about the GPA {1}”, Name,pGPAScore.ToString()));
}
}
No comments:
Post a Comment