Esempio n. 1
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();

        }
Esempio n. 2
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();
        }
Esempio n. 4
0
        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();
        }
Esempio n. 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();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

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

            // register the handler, note anonymous class. Note the arg as a handler
            // that conforms to the delegate method signature
            // If you change the method handler arguments below, you will get a compile time error.

            myCar.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later.
            // The Register function keeps a list of all the event handlers
            //  that conform to the delegate signature.

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

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

            // Unregister from the second handler.
            myCar.UnRegisterWithCarEngine(handler2);

            // We won't see the 'uppercase' message anymore!
            Console.WriteLine("***** Speeding up  but unregistered 2nd event handler*****");
            for (int i = 0; i < 6; i++)
            {
                myCar.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Delegates as event enablers ****");

            Car car = new Car("Jaguar", 100, 10);

            car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Console.WriteLine("*** Speeding UP!!! ***");
            for (int i = 0; i < 6; i++)
            {
                car.Accelerate(20);
            }
        }
Esempio n. 8
0
        static void AsEvent()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("=> Delegates as event enablers");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Console.WriteLine("===== Speeding up =====");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }
        }
Esempio n. 9
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();
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            Console.WriteLine("***********Delegate as event enablers*********\n");
            Car car = new Car("SlugBug", 100, 10);

            car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Console.WriteLine("Speeding Up");
            for (int i = 0; i < 6; i++)
            {
                car.Accelerate(20);
                Console.ReadLine();
            }
        }
Esempio n. 11
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);

            // Register multiple targets for the notifications.
            Car.CarEngineHandler chdl1 = new Car.CarEngineHandler(OnCarEngineEvent);
            c1.RegisterWithCarEngine(chdl1);


            // 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 'upper case' message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            // Console.WriteLine("***** Delegates as event enablers *****\n");
            //
            // Car c1 = new Car("Slugbug", 100, 10);
            // c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            //
            // Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            // c1.RegisterWithCarEngine(handler2);
            //
            // Console.WriteLine("***** Speeding up *****");
            // for (int i = 0; i < 6; i++)
            // {
            //     c1.Accelerate(20);
            // }
            //
            // c1.UnRegisterWithCarEngine(handler2);
            //
            // Console.WriteLine("***** Speeding up *****");
            // for (int i = 0; i < 6; i++)
            // {
            //     c1.Accelerate(20);
            // }

            Console.WriteLine("***** Method Group Conversion *****\n");
            Car c2 = new Car();

            c2.RegisterWithCarEngine(OnCarEngineEvent);

            Console.WriteLine("***** Speeding Up *****");
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }

            c2.UnRegisterWithCarEngine(OnCarEngineEvent);

            // No more notifications!
            Console.WriteLine("No more notifications!");
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }

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

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerat(20);
            }
            c1.UnRegisterWithCarEngine(handler2);
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerat(20);
            }
            Console.ReadLine();
        }
Esempio n. 14
0
        //  1. define a new delegate type that will be used to send notifications to the caller
        //  2. declare a member variable of this delegate in teh Car class
        //  3. create a helper function on the Car that allows the caller to specify the method to call back on
        //  4. implement the Accelerate() method to invoke the delegate's invocation list under the correct circumstances

        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as Event Enablers *****");

            //  first make a Car object
            Car freddy = new Car("Freddy", 100, 20);

            //  next tell the car which method to call when it wants to send us messages
            freddy.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //  speed up to trigger the events
            Console.WriteLine("***** Speeding up... *****");
            for (int i = 1; i < 11; i++)
            {
                freddy.Accelerate(10);
            }


            Console.WriteLine("Press <Enter> to exit. . .");
            Console.ReadLine();
        }
Esempio n. 15
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();
 }