p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=222)
-   -   Absolute Beginers Comprehension of Events (http://p2p.wrox.com/showthread.php?t=48763)

webworldman October 7th, 2006 01:00 PM

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.



All times are GMT -4. The time now is 05:54 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.