Beispiel #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);
            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);

            // We won't see the 'uppercase' message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();
        }
        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();
        }
Beispiel #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();

        }
        static void Main(string[] args) {
            Car c1 = new Car("SlugBug", 100, 10);
            //c1.RegisterWithCarEngine(OnCarEngineEvent);          Also works fine, method group conversion
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

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

            c1.UnRegisterWithCarEngine(handler2);
            for (int i = 0; i < 2; i++) {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            // First, make a Car Object
            Car c1 = new Car("SlougBug", 100, 10);

            // Now, tell the car which method to call
            // when it wants to send us messages.
            // Extra-register multiple targets for the notifications
            c1.RegisterWithCarEngine(new Car.CarEngineHanlder(OnCarEngineEvent));
            // c1 i                           => the event source object
            // .RegisterWithCarEngine         => provides the registration/helper function
            // new Car.CarEngineHandler(...   => Listener Object                                            (EVENT HANDLER)
            // OnCarEngineEvent               => Event Handler Method / Listner Handler Object's method     (EVENT HANDLER)

            Car.CarEngineHanlder handler2 = new Car.CarEngineHanlder(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);

            //--------------------------Water Bottle---------------------------------------//
            PlasticWaterBottle myBottle = new PlasticWaterBottle(aName:"DANASI");
            //myBottle.RegisterWithBottleOfWater(new PlasticWaterBottle.WaterNotification(MartinBottle)); // OLD WAY
            myBottle.RegisterSubscribers(MartinBottle01); // NEW WAY

            // Lets fill the water
            for (int i = 0; i < 20; i++) {
                myBottle.fillWater(i);
            }

            Console.ReadLine();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            //make a car
            Car c1 = new Car("SlugBug", 100, 10);
            //now tell the car which method(s) to call
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //we want an object that we can subsequently UNREGISTER...
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            //UNREGISTER the second handler
            c1.UnRegisterWithCarEngine(handler2);//enabling this line will UNREGISTER the second
            //event

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

            Console.ReadLine();
        }
Beispiel #7
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 wants to send us messages.
     // c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
     c1.RegisterWithCarEngine(OnCarEngineEvent); // shortcut for pervious line
     // c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));
     c1.RegisterWithCarEngine(OnCarEngineEvent2);
     // Speed up (this will trigger the events).
     Console.WriteLine("***** Speeding up *****");
     for (int i = 0; i < 6; i++)
         c1.Accelerate(20);
     Console.ReadLine();
 }