PROGRAMMING IN C#
Module 12: Events and Delegates
Lab Guide for Lab6
Session Objectives
In this session, you will be practicing with
Create and fire an event
Subcribing to event
Part 1 – Getting started (30 minutes)
1. Creating a delegate and subscribe some methods to it.
Declare delegate Incremen:
void Increment(ref int X);
Define 2 methods that increment a variable to 2 and 3, subcribe these methods to a declared delegate and
invoke these methods through the delegate
Scan the code first, type the code, compile, run and observe the result.
1. Create class Program
class Program
{
public void Increment(ref int X);
public static void Add2(ref int x)
{
x += 2;
}
public static void Add3(ref int x)
{
x += 3;
}
public static void Main()
{
Increment functionDelegate = Add2;
functionDelegate += Add3;
functionDelegate += Add2;
int x = 5;
functionDelegate(ref x);
Console.ReadLine();
}
}
2. Create an application that simulate some devices like air conditioner must do something when the
environment’s temperature changes. So create class Thermostat that monitor environment’s temperature.
It has an event where other devices will subcribe to it.
Scan the code first, type the code, compile, run and observe the result.
1. Class Thermostat
class Thermostat
{
public delegate void TemperatureChangeHandler(
float newTemperature);
// Define the event publisher
private TemperatureChangeHandler _OnTemperatureChange;
public TemperatureChangeHandler OnTemperatureChange
{
get { return _OnTemperatureChange; }
set { _OnTemperatureChange = value; }
}
public float CurrentTemperature
{
get { return _CurrentTemperature; }
set
{
if (value != CurrentTemperature)
{
_CurrentTemperature = value;
// Call subscribers
if (OnTemperatureChange!=null)
{
OnTemperatureChange(value);
}
}
}
}
private float _CurrentTemperature;
}
2.
Class Cooler
class Cooler
{
public Cooler(float temperature)
{
Temperature = temperature;
}
public float Temperature
{
get { return _Temperature; }
set { _Temperature = value; }
}
private float _Temperature;
public void OnTemperatureChanged(float newTemperature)
{
if (newTemperature > Temperature)
{
System.Console.WriteLine("Cooler: On");
}
else
{
System.Console.WriteLine("Cooler: Off");
}
}
}
3.
Class Heater
class Heater
{
public Heater(float temperature)
{
Temperature = temperature;
}
public float Temperature
{
get { return _Temperature; }
set { _Temperature = value; }
}
private float _Temperature;
public void OnTemperatureChanged(float newTemperature)
{
if (newTemperature < Temperature)
{
System.Console.WriteLine("Heater: On");
}
else
{
System.Console.WriteLine("Heater: Off");
}
}
}
4.
Class Program
class Program
{
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater(60);
thermostat.OnTemperatureChange +=
heater.OnTemperatureChanged;
Heater heaterCuong = new Heater(100);
thermostat.OnTemperatureChange +=
heaterCuong.OnTemperatureChanged;
Cooler cooler = new Cooler(80);
thermostat.OnTemperatureChange +=
cooler.OnTemperatureChanged;
string temperature;
Console.Write("Enter temperature: ");
temperature = Console.ReadLine();
thermostat.CurrentTemperature = int.Parse(temperature);
Console.ReadLine();
}
}
Part 2 – Workshops (30 minutes)
•
•
Quickly look at workshops of Module 12
Try to compile, run and observe the output of sample code provided for related workshop. Discuss with
your class-mate and your instructor if needed.
Part 3 – Lab Assignment (60 minutes)
Do the assignment for Module 12 carefully. Discuss with your class-mates and your instructor if needed.
Part 4 – Do it your self