Absolute Beginers Comprehension of Events
using System;
using System.Collections.Generic;
using System.Text;
namespace BeautyEvents
{
public delegate void BeautyActionHandler();
public class Beauty
{
public string _name;
public Beauty(string name)
{
_name = name;
}
public event BeautyActionHandler TakesBath;
public void TakingBathHappens()
{
if (TakesBath != null)
{
Console.WriteLine("{0} is taking a bath.", _name);
this.TakesBath();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BeautyEvents
{
public class BathRoom
{
public void ShutDoor()
{
Console.WriteLine("BothRoom door shut!");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BeautyEvents
{
class Program
{
static void Main(string[] args)
{
Beauty Mary = new Beauty("Mary");
BathRoom XFBathRoom = new BathRoom();
Mary.TakesBath += new BeautyActionHandler(XFBathRoom.ShutDoor);
Mary.TakingBathHappens();
Console.ReadKey();
}
}
}
// 1. Create a delegate
// 2. create an event
// 3. create a method to fire the event.
// 4. Create an event handler in a event handling class.
// 5. Initializing the delegate in the client code.
// 6. Join the the event and the instance of the delegate with the +=
// operator (passing in the event handler method)
// 7. type in the event firing method and run the aplication.
|