using System;
using System.Text;
using System.Collections;

public class Car : Object
{
      // the delegate
      public delegate void CarDelegate(Car c);
      // for the delegating property
      private bool isDirty;
      private bool shouldRotate;
     
      // implementing events
      public static event EngineHandler Exploded;
      public static event EngineHandler AboutToBlow;
      public delegate void EngineHandler(string msg);

      private int currSpeed;
      bool dead;
      private int maxSpeed;
      private string petName;
      // property for the new fields
      public bool Rotate
      {
            get{ return shouldRotate; }
            set{ shouldRotate=value; }
      }

      public bool Dirty
      {
            get{ return isDirty; }
            set{ isDirty = value; }
      }

      public int CurrSpeed
      {
            get{ return currSpeed; }
      }

      public string PetName
      {
            get{return petName;}
      }
     
      public Car()
      {
            maxSpeed = 100;
            dead = false;
      }
     
      public Car( string name, int max, int curr ,
            bool isDirty, bool shouldRotate )
      {
            currSpeed = curr;
            maxSpeed = max;
            petName = name;
            dead = false;
            this.isDirty = isDirty;
            this.shouldRotate = shouldRotate;
      }
     
      public void SpeedUp( int delta )
      {
            if(dead)
            {
                  if(Exploded != null)Exploded("Sorry, this car
is dead...");
            }
            else
            {
                  currSpeed += delta;
                  if(10==(currSpeed-maxSpeed))
                        if(AboutToBlow != null)AboutToBlow
("Be carefull!");
                  if(currSpeed >= maxSpeed)dead = true;
                  else Console.WriteLine( "Current Speed is=
{0}", currSpeed);
            }
      }
     
      public void GetInfo()
      {
            Console.WriteLine("Car name is {0}, the MaxSpeed is
{1}", petName, maxSpeed);
            Console.WriteLine("The current speed is {0}",
currSpeed );
      }
}

public class Garage
{
      private      ArrayList theCars = new ArrayList();
      public Garage()
      {
            theCars.Add(new Car("Fee-Fee", 400, 80, false, true));
            theCars.Add(new Car("Fee-Fee", 240, 120,false, true));
      }

      public void ProcessCars(Car.CarDelegate proc)
      {
            Console.WriteLine("Calling: {0}", proc.Method.ToString
());
            if(proc.Target != null)Console.WriteLine("Nonstatic
method");
            else Console.WriteLine("A static method");
            foreach(Car car in theCars)
                  proc(car);
      }
}

public class CarApp
{
      public CarApp(){}
      public static void WashCar(Car c)
      {
            if(c.Dirty)Console.WriteLine("Cleaning a {0} Car",
c.PetName);
            else Console.WriteLine("This car {0} is clean",
c.PetName);
      }

      public static void RotateTires(Car c)
      {
            if(c.Rotate)Console.WriteLine("Tires have been
rotating in {0}", c.PetName);
            else Console.WriteLine("Don't need to be rotated in
{0}...", c.PetName);
      }
}

public class MainClass
{
      /*
      public static void WashCar(Car c)
      {
            if(c.Dirty)Console.WriteLine("Cleaning a {0} Car",
c.PetName);
            else Console.WriteLine("This car {0} is clean",
c.PetName);
      }

      public static void RotateTires(Car c)
      {
            if(c.Rotate)Console.WriteLine("Tires have been
rotating in {0}", c.PetName);
            else Console.WriteLine("Don't need to be rotated in
{0}...", c.PetName);
      }
      */
      public static void Main()
      {
           
            Garage g = new Garage();
            CarApp App = new CarApp();
// How can I force to work this string.
// Error
            Car.CarDelegate wash = new Car.CarDelegate
(App.WashCar);
            g.ProcessCars(wash);
            /*
            g.ProcessCars(new Car.CarDelegate(RotateTires));*/
           
      }     
}





Yahoo! Groups Sponsor
ADVERTISEMENT
click here
Web Bug from http://us.adserver.yahoo.com/l?M=315388.5529720.6602079.3001176/D=groups/S=:HM/A=2372354/rand=770120652


Yahoo! Groups Links

Reply via email to