Event driven programming in C#

Before reading this article, I suggest that you to read my ABC of Delegates article.

What is an event? Event means something we do like posting this article, reading this article etc. Everything we do, is an event. In order to illustrate event driven programming, I would like to tell a story and show how I can explain this real life scenario using events. So start thinking OOPs.

During my schooling, I was very worried about my semester result time. Why? I was not a good student and I used to play whole day which generally reflected in my semester result. My parents scolded me a lot for these activities. Anyway I informed them about my result.

In the above problem, we can easily understand that two objects are 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 do we use this delegate? Because we need to call Parent's NotifyMe method. So the delegate signature would be identical with the NotifyMe method'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 void RecordGPAScore()
    {
        if (NotifyToParent != null)
            NotifyToParent(GPAScore);
    }
}

Here we need to understand that Student calls the method NotifyMe of Parent class. So we need to declare a delegate which will point to the NotifyMe method.

Here someone can argue that why don't I instantiate one Parent object and call the NotifyMe method within RecordGPAScore method. I would say, this can create a dependency between Student class and Parent Class. Suppose in the future, Student class needs to notify another class like Teacher 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 has OnClick event. Now if we write any specific code within Button class, would it be reusable? So better define delegate, then an event of that delegate type and tell to developers that the method you want to call, should match this delegate's signature. The same thing we are doing here. We defined one delegate NotifyGPADelegate, then an event NotifyToParent of that delegate type and ask developers to subscribe to this event.

Here how we wire up these two objects

Student oStudent = new Student();
oStudent.Name = "James";
oStudent.GPAScore = 80;

Parent oParent = new Parent();
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 NotifyToParent event with the method NotifyMe of Parent class. At last we call the method to RecordGPAScore, which in turn calls the NotifyMe of Parent class.

Sample code looks like

class Program
{       
   static void Main(string[] args)
    {
        Student oStudent = new Student();
        oStudent.Name = "James";
        oStudent.GPAScore = 80;

        Parent oParent = new Parent();
        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 void RecordGPAScore()
    {
        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()));
    }
}

delegate.gif


Similar Articles