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();
// An error will increase.How to force this string to work correctly
//If you uncomment in the Main class static functions 
// and try to execute program with a string 
//g.ProcessCars(new Car.CarDelegate(RotateTires));
// it will work
                Car.CarDelegate wash = new Car.CarDelegate
(App.WashCar);
                g.ProcessCars(wash);
        }       
}






------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/XGgtlB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/Microsofts_C_Sharp/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to