예제 #1
0
        private void tank_Underflow(Object sender, TankEventArgs e)
        {
            string message   = e.Message;
            int    deficency = e.Amount;

            MessageBox.Show("Message: " + message + "\n" + "Deficency: " + deficency);
        }
예제 #2
0
        //method to add water to the tank
        public void AddWater(int amount)
        {
            //as we add water, the current level could
            //exceed the maxLevel. At that point, we need
            //to alert or notify the client implies fire
            //the Overflow event, which invokes the delegate
            //object, and the delegate calls a method in the
            //client.

            if (amount + _currentLevel > _maxLevel)
            {
                if (Overflow != null) //check that a client has registered to this event
                {
                    //We have exceeded the max, fire the event
                    int           excess  = Math.Abs(_maxLevel - (amount + _currentLevel));
                    string        message = "Would cause the tank to overflow";
                    TankEventArgs te      = new TankEventArgs(message, excess);
                    //Fire the event: same syntax as invoking a delegate
                    Overflow(this, te);
                }
            }
            else
            {
                _currentLevel += amount;
            }
        }
예제 #3
0
        //method that handles the Overflow event when it gets fired from the tank
        //Must have the same signature as the delegate type (TankHandler)
        private void tank_Overflow(Object sender, TankEventArgs e)
        {
            string message = e.Message;
            int    excess  = e.Amount;

            MessageBox.Show("Message: " + message + "\n" + "Excess: " + excess);
        }
예제 #4
0
 //method to use or remove water from the tank
 public void UseWater(int amount)
 {
     if (_currentLevel - amount < _minLevel)
     {
         if (Underflow != null)
         {
             int           deficiency = _minLevel - (amount - _currentLevel);
             string        message    = "Would cause the tank to underflow";
             TankEventArgs te         = new TankEventArgs(message, deficiency);
             Underflow(this, te);
         }
     }
     else
     {
         _currentLevel -= amount;
     }
 }