Simple Event Driven Programming with C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace event_driven_console
{
public delegate void NotifyGpaDelegate(int GPAScore); // *
class Student
{
public event NotifyGpaDelegate Notify; // *
public String Name { get; set; }
private int gpaScore;
public int GPAScore
{
get { return gpaScore; }
set
{
gpaScore = value;
if (Notify != null)
Notify(GPAScore);
}
}
}
class Teacher
{
public string Name { get; set; }
public void Notify(int pGPAScore)
{
Console.WriteLine(
String.Format("Hello {0}, Your student take the {1} score",
Name, pGPAScore));
}
}
class Parent
{
public String Name { get; set; }
public void Notify(int pGPAScore)
{
Console.WriteLine(
String.Format("{0} notified about the GPA {1}",
Name, pGPAScore));
}
}
class Program
{
static void Main(string[] args)
{
var student = new Student();
student.Name = "James";
var parent = new Parent();
parent.Name = "Daddy Cool";
var mathteacher = new Teacher
{
Name = "Math Teacher",
};
var physicsteacher = new Teacher
{
Name = "Physics Teacher",
};
student.Notify += new NotifyGpaDelegate(parent.Notify);
student.Notify += new NotifyGpaDelegate(mathteacher.Notify);
student.Notify += new NotifyGpaDelegate(physicsteacher.Notify);
student.GPAScore = 80;
Console.Read();
}
}
}
Daddy Cool notified about the GPA 80
Hello Math Teacher, Your student take the 80 score
Hello Physics Teacher, Your student take the 80 score