Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call
            // when it wats to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Unregister from the second handler.
            c1.UnregisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Delegates as even enablers ****\n");

            // First, make a car object
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call when it wants to send us messages
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            // This time hold onto the delegate object, so that it can be unregistered later
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2); // unregister the delegate object

            // Speed up (this will trigger the events)
            Console.WriteLine("**** Speeding UUUUUUP! *****\n");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnregisterWithCarEngine(handler2);
            c1.CurrentSpeed = 0;

            Console.WriteLine("**** Speeding UUUUUUP! *****\n");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            Car c1 = new Car("Slug Bug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEventHandler(OnCarEngineEvent));
            Car.CarEventHandler handler2 = new Car.CarEventHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            Console.WriteLine("***** Speeding Up *****");

            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.WriteLine("\n");

            c1.UnregisterWithCarEngine(handler2);
            
            c1.ResetCar();
            c1.CurrentSpeed = 10;
            
            Console.WriteLine("***** Speeding Up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();

        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Car carAlfaRomeo = new Car("AlfaRomeo", 100, 10);

            carAlfaRomeo.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler carHandler = new Car.CarEngineHandler(OnCarEngineEventNew);
            carAlfaRomeo.RegisterWithCarEngine(carHandler);
            Console.WriteLine("Speeding up!");
            for (int i = 0; i < 6; i++)
            {
                carAlfaRomeo.Accelerate(20);
            }
            carAlfaRomeo.UnregisterWithCarEngine(carHandler);
        }