Example #1
0
 // 4) Implement the Accelerate() method to invoke the delegate's
 // invocation list under the correct circumstances.
 public void Accelerate(int delta)
 {
     // If this car is "dead," send dead message.
     if (carIsDead)
     {
         Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
     }
     else
     {
         CurrentSpeed += delta;
         // Is this car "almost dead"?
         if (10 == (MaxSpeed - CurrentSpeed))
         {
             AboutToBlow?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
         }
         if (CurrentSpeed >= MaxSpeed)
         {
             carIsDead = true;
         }
         else
         {
             Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
         }
     }
 }
Example #2
0
        public void Accelerate(int delta)
        {
            // If the car is dead, fire Exploded event.
            if (carIsDead)
            {
                Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
            }
            else
            {
                CurrentSpeed += delta;

                // Almost dead?
                if (10 == MaxSpeed - CurrentSpeed)
                {
                    AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy!  Gonna blow!"));
                }

                // Still OK!
                if (CurrentSpeed >= MaxSpeed)
                {
                    carIsDead = true;
                }
                else
                {
                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
Example #3
0
        /// <exception cref="Exception">A delegate callback throws an exception.</exception>
        /// <exception cref="IOException">Произошла ошибка ввода-вывода. </exception>
        /// <exception cref="ArgumentNullException">Параметр <paramref name="format" /> имеет значение null. </exception>
        /// <exception cref="FormatException">Заданная в параметре <paramref name="format" /> спецификация формата недопустима. </exception>
        public void Accelerate(int delta)
        {
            // send message if engine is broken
            if (CarIsDead || CurrentSpeed + delta > MaxSpeed)
            {
                // using null propagation
                //_listOfHandlers?.Invoke ("Car engine is broken...");
                // the same as:
                // if (_listOfHandlers != null) _listOfHandlers("Car engine is broken...");

                // the same using events:
                Exploded?.Invoke(this, new CarEventArgs("Car engine is broken..."));
            }
            else
            {
                CurrentSpeed += delta;
                if (10 == (MaxSpeed - CurrentSpeed))
                {
                    //_listOfHandlers?.Invoke ("Careful buddy! Gonna blow!");
                    // the same using events:
                    AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy! Gonna blow!"));
                }
                if (CurrentSpeed >= MaxSpeed)
                {
                    CarIsDead = true;
                }
                else
                {
                    Console.WriteLine("Current car speed - {0}", CurrentSpeed);
                }
            }
        }
Example #4
0
        // private CarEngineHandlet listofHandlers;

        //public void RegisterCarEngine(CarEngineHandlet methodCall)
        //{
        //    listofHandlers += methodCall;
        //}

        public void Accelerate(int delta)
        {
            if (CarIsDead)
            {
                if (Exploded != null)
                {
                    //listofHandlers("car is dead");
                    Exploded.Invoke("Sorry car is dead");
                }
            }
            else
            {
                this.CurrentSpeed += delta;
                if (10 == (MaxtSpeed - CurrentSpeed) && AboutToBlow != null)
                {
                    AboutToBlow.Invoke("Careful");
                }
                if (CurrentSpeed >= MaxtSpeed)
                {
                    CarIsDead = true;
                }
                else
                {
                    Console.WriteLine("Current speed: {0}", CurrentSpeed);
                }
            }
        }
Example #5
0
 //Implement the Accelerate() method to invoke the delegate's
 //invocation list under the correct circumstances.
 public void Accelerate(int delta)
 {
     //if car is "dead" fire exploded event
     if (IsCarDead)
     {
         Exploded?.Invoke("Sorry, this car is dead!");
     }
     else
     {
         currentSpeed += delta;
         //car is almost dead
         if (10 == MaxSpeed - currentSpeed)
         {
             AboutToBlow?.Invoke("Careful buddy! Gonna blow!");
         }
         if (currentSpeed >= MaxSpeed)
         {
             IsCarDead = true;
         }
         else
         {
             Console.WriteLine($"CurrentSpeed: {currentSpeed}");
         }
     }
 }
Example #6
0
        public void Accelerate(int delta)
        {
            if (_carIsDead)
            {
                Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
            }
            else
            {
                CurrentSpeed += delta;

                if (10 == MaxSpeed - CurrentSpeed)
                {
                    AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy! Gonna blow!"));
                }

                if (CurrentSpeed >= MaxSpeed)
                {
                    _carIsDead = true;
                }
                else
                {
                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
        /// <summary>
        /// Accelerate the car by an amount until max is reached, then explicit events are called
        /// </summary>
        /// <param name="delta"></param>
        public void AccelerateUsingExplicitEvents(int delta)
        {
            // If this car is 'dead', fire Exploded event
            if (CurrentSpeed >= MaxSpeed)
            {
                string message = $"Exploded {Name} Speed {CurrentSpeed}, Accelerate {delta}";
                Exploded?.Invoke(message);
                ExplodedEvent?.Invoke(this, new CarEventArgs(message));
                GenericExplodedEvent?.Invoke(this, new CarEventArgs(message));
            }
            else
            {
                CurrentSpeed += delta;

                // Is this car 'almost dead'?
                if ((MaxSpeed - CurrentSpeed) == 10)
                {
                    // fire AboutToBlow event
                    string message = $"AboutToBlow {Name} Speed {CurrentSpeed}, Accelerate {delta}";
                    AboutToBlow?.Invoke(message);
                    AboutToBlowEvent?.Invoke(this, new CarEventArgs(message));
                    GenericAboutToBlowEvent?.Invoke(this, new CarEventArgs(message));
                }
                else
                {
                    Console.WriteLine($"Accelerating {Name} Speed {CurrentSpeed}, Accelerate {delta}");
                }
            }
        }
Example #8
0
        //1. Определить новый тип делегата, который будет использоваться
        //   для отправки уведомлений вызывающему коду.

        //2. Объявить переменную-член этого типа делегата в классе Саг.
        //private CarEngineHandler listOfHandlers;

        //3. Создать в классе Саг вспомогательную функцию для вызывающего кода
        //public void RegisterWithCarEngine(CarEngineHandler methodtocall)
        //{
        //    listOfHandlers += methodtocall;
        //}
        //Удаляет функцию из списка
        //public void UnRegisterWithCarEngine(CarEngineHandler methodtocall)
        //{
        //    listOfHandlers -= methodtocall;
        //}
        //4. Реализовать метод Accelerate () для обращения к списку вызовов
        //   делегата в подходящих обстоятельствах.

        public void Accelerate(int delta)
        {
            // Если этот автомобиль сломан, то отправить сообщение об этом,
            if (carIsDead)
            {
                Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
            }
            else
            {
                CurrentSpeed += delta;
                // Автомобиль почти сломан?
                if (10 == (MaxSpeed - CurrentSpeed))
                {
                    AboutToBlow?.Invoke(this,
                                        new CarEventArgs("Careful buddy! Gonna blow!"));
                }
                if (CurrentSpeed >= MaxSpeed)
                {
                    carIsDead = true;
                }
                else
                {
                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
Example #9
0
        public void Accelerate2(int delta)
        {
            if (carIsDead)
            {
                Exploaded?.Invoke("Sorry, this car is dead...");
            }
            else
            {
                Speed += delta;

                if (10 >= (MaxSpeed - Speed))
                {
                    AboutToBlow?.Invoke("Careful buddy! Gonna blow!");
                }
            }

            if (Speed >= MaxSpeed)
            {
                carIsDead = true;
            }
            else
            {
                Console.WriteLine($"Current Speed = {Speed}");
            }
        }
        public void Accelerate(int delta)
        {
            if (carIsDead)
            {
                // Also, you can do like follows using null conditional operator
                Exploded?.Invoke(this, new CarEventArgs("Sorry, this CarWithGenericEventHandle is dead..."));
            }
            else
            {
                CurrentSpeed += delta;

                // Also, you can do like follows using null conditional operator
                if (10 == (MaxSpeed - CurrentSpeed))
                {
                    AboutToBlow?.Invoke(this, new CarEventArgs("CarWithGenericEventHandleeful buddy! Gonna blow!"));
                }

                if (CurrentSpeed >= MaxSpeed)
                {
                    carIsDead = true;
                }
                else
                {
                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
Example #11
0
        public void Acclerate(int delta)
        {
            //Если автомобиль сломан инициировать событие Exploded
            if (carIsDead)
            {
                //Сокращение кода
                //Exploded?.Invoke("Sorry, this car is dead...");

                //Т.к. создан свой класс CarEventArgs, то вызов события меняется
                Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
            }
            else
            {
                CurrentSpeed += delta;

                //Почти сломался?
                if (10 == MaxSpeed - CurrentSpeed)
                {
                    AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy! Gonna Blow!"));
                }


                //Всё ещё впорядке!
                if (CurrentSpeed >= MaxSpeed)
                {
                    carIsDead = true;
                }
                else
                {
                    Console.WriteLine("CurrentSpeed={0}", CurrentSpeed);
                }
            }
        }
Example #12
0
        //  Define a member variable of this delegate.
        //private CarEngineHandler listOfHandlers;

        //  Add registration function for the caller.
        //public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        //{
        //    listOfHandlers += methodToCall;
        //}

        //public void UnRegisterWithCarEngine(CarEngineHandler methoToCall)
        //{
        //    listOfHandlers -= methoToCall;
        //}

        //  Implement the Accelerate() method to invoke the delegate's
        //  invocation list under the correct circumstatnces.
        public void Accelerate(int delta)
        {
            //  If the car is "dead", send dead message.
            if (carIsDead)
            {   //  Can use null conditional operator.
                //  Must explicitly call invoke when doing so.
                //if(Exploded != null)
                //Exploded?.Invoke("Sorry, this car is dead...");
                //  Using EventArgs
                Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
            }
            else
            {
                CurrentSpeed += delta;

                //  Can use null conditional operator
                //  Must explicitly call Invoke when doing so.
                //  Is the car "almost dead"?
                if (10 == (MaxSpeed - CurrentSpeed))// && AboutToBlow != null)
                {
                    //AboutToBlow?.Invoke("Careful buddy! Gonna blow!");
                    //  Using EventArgs
                    AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy! Gonna blow!"));
                }
                if (CurrentSpeed >= MaxSpeed)
                {
                    carIsDead = true;
                }
                else
                {
                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
Example #13
0
 public void Accelerate(int delta)
 {
     if (carIsDead)
     {
         //  if this car is dead send dead message
         Exploded?.Invoke(this, new CarEventArgs("Sorry, this car is dead..."));
     }
     //  is this car almost dead
     else if (10 == MaxSpeed - CurrentSpeed)
     {
         AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy! Gonna blow!"));
     }
     else if (CurrentSpeed >= MaxSpeed)
     {
         carIsDead = true;
     }
     else
     {
         Console.WriteLine($"Current Speed = { CurrentSpeed }.");
     }
     CurrentSpeed += delta;
 }
Example #14
0
 /*
  * public void RegisterWithCarEngine(CarEngineHandle methodToCall)
  * {
  *      listOfHandlers += methodToCall;
  * }
  *
  * public void UnRegisterWithCarEngine(CarEngineHandle methodToCall)
  * {
  *      listOfHandlers -= methodToCall;
  * }
  */
 public void Accelerate(Int32 delta)
 {
     if (carIdDead)
     {
         Exploded?.Invoke(this, new CarEventArg("Sorry, this car is dead..."));
     }
     else
     {
         CurrentSpeed += delta;
         if (MaxSpeed - CurrentSpeed <= 10)
         {
             AboutToBlow?.Invoke(this, new CarEventArg("Careful buddy! Gonna blow!"));
         }
         if (CurrentSpeed > MaxSpeed)
         {
             this.carIdDead = true;
         }
         else
         {
             Console.WriteLine($"CurrentSpeed = {this.CurrentSpeed}");
         }
     }
 }
 public void Accelerate(int delta)
 {
     if (carIsDead)
     {
         Exploded?.Invoke(this, new CarEventArgs("Car is dead..."));
     }
     else
     {
         CurrentSpeed += delta;
         if (10 >= (MaxSpeed - CurrentSpeed))
         {
             AboutToBlow?.Invoke(this, new CarEventArgs("Careful buddy! Car is gonna blow..."));
         }
         if (CurrentSpeed > MaxSpeed)
         {
             carIsDead = true;
             Exploded?.Invoke(this, new CarEventArgs("Car is dead..."));
         }
         else
         {
             System.Console.WriteLine("CurrentSpeed is : {0}", CurrentSpeed);
         }
     }
 }
Example #16
0
 // Remove member to the invocation list.
 public void RemoveAboutToBlow(AboutToBlow clientMethod)
 {
     almostDeadList -= clientMethod;
 }
Example #17
0
 // Add member to the invocation list.
 public void OnAboutToBlow(AboutToBlow clientMethod)
 {
     almostDeadList += clientMethod;
 }
Example #18
0
 public void OnAboutToBlow(AboutToBlow clienMeth)
 {
     almostDeadList = clienMeth;
 }
Example #19
0
 public void onAboutToBlow(AboutToBlow e)
 {
     aboutToBlowListener += e;
 }