locked
GetEnumerator RRS feed

  • Question

  •  

    public class Car

    {

     

        public const int maxSpeed = 100;

     

        private int currSpeed;

        private string petName;

     

        private bool carIsDead;

     

     

        public Car() { }

        public Car(string name, int currSp)

        {

            currSpeed = currSp;

            petName = name;

        }

        public void CrankTunes(bool state)

        {

     

        }

     

        public void Accelerate(int delta)

        {

            if (carIsDead)

                Console.WriteLine("{0} is out of order...", petName);

            else

            {

                currSpeed += delta;

                if (currSpeed > maxSpeed)

                {

                    Console.WriteLine("{0} has overheated!", petName);

                    currSpeed = 0;

                    carIsDead = true;

                }

                else

                    Console.WriteLine("=> CurrSpeed = {0}", currSpeed);

            }

        }

    }

     

    public class Cars

    {

        private Car[] carArray;

        public Cars()

        {

            carArray = new Car[4];            //  Car("FeeFee",200);

            carArray[0]=new Car("FeeFee",200);

            carArray[1] = new Car("clunker",90);

            carArray[2] = new Car("Fred", 200);

            carArray[3] = new Car("zippy", 90);

     

        }

    }

     

     

     

     

     

    public class Driver

    {

        public static void Main()

        {

     

            Cars carLot = new Cars();

            foreach(Car c in carLot)// foreach operator can't operate on variables of type Cars because Cars doesn't contain a public definition of GetEnumerator

    // remaining part.......

     

     

     

        }

     

     

     

     

    }

     

     

    why i am getting this error?????


    regards, sushma rai http://in.linkedin.com/pub/sushma-rai/28/bb0/988
    Friday, September 16, 2011 4:57 PM

Answers

  • Car class is not an enumerable class.

    Make it enumerable: http://support.microsoft.com/kb/322022

     

    if you just want to go through each car object you created create a List<car> instead of an array.

     

    cs

    • Edited by CountryStyle Friday, September 16, 2011 5:05 PM
    • Proposed as answer by CountryStyle Friday, September 16, 2011 5:05 PM
    • Marked as answer by sushma rai Friday, September 16, 2011 5:12 PM
    Friday, September 16, 2011 5:02 PM
  • If you want to make Cars enumerable with foreach,  you should to implement IEnumerable<Car>:

    public class Cars : IEnumerable<Car>
    {
        private Car[] carArray;
        public Cars()
        {
            carArray = new Car[4];            //  Car("FeeFee",200);
            carArray[0]=new Car("FeeFee",200);
            carArray[1] = new Car("clunker",90);
            carArray[2] = new Car("Fred", 200);
            carArray[3] = new Car("zippy", 90);
       }
    
       // Just use the array's enumerator
       public IEnumerator<Car> GetEnumerator()
       {
          return carArray.GetEnumerator();
       }
    
       public IEnumerator GetEnumerator()
       {
          return carArray.GetEnumerator();
       }
    }
    


    That being said, I'd also recommend considering switching this to using a List<T> internally.  It makes it easier if you later want to add more cars:

    public class Cars : IEnumerable<Car>
    {
        private List<Car> cars;
        public Cars()
        {
            cars = new List<Car>();
            cars.Add(new Car("FeeFee",200));
            cars.Add(new Car("clunker",90));
            cars.Add(new Car("Fred", 200));
            cars.Add(new Car("zippy", 90));
       }
    
       // Just use the array's enumerator
       public IEnumerator<Car> GetEnumerator()
       {
          return cars.GetEnumerator();
       }
    
       public IEnumerator GetEnumerator()
       {
          return cars.GetEnumerator();
       }
    }
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by sushma rai Friday, September 16, 2011 5:13 PM
    Friday, September 16, 2011 5:06 PM

All replies

  • Car class is not an enumerable class.

    Make it enumerable: http://support.microsoft.com/kb/322022

     

    if you just want to go through each car object you created create a List<car> instead of an array.

     

    cs

    • Edited by CountryStyle Friday, September 16, 2011 5:05 PM
    • Proposed as answer by CountryStyle Friday, September 16, 2011 5:05 PM
    • Marked as answer by sushma rai Friday, September 16, 2011 5:12 PM
    Friday, September 16, 2011 5:02 PM
  • If you want to make Cars enumerable with foreach,  you should to implement IEnumerable<Car>:

    public class Cars : IEnumerable<Car>
    {
        private Car[] carArray;
        public Cars()
        {
            carArray = new Car[4];            //  Car("FeeFee",200);
            carArray[0]=new Car("FeeFee",200);
            carArray[1] = new Car("clunker",90);
            carArray[2] = new Car("Fred", 200);
            carArray[3] = new Car("zippy", 90);
       }
    
       // Just use the array's enumerator
       public IEnumerator<Car> GetEnumerator()
       {
          return carArray.GetEnumerator();
       }
    
       public IEnumerator GetEnumerator()
       {
          return carArray.GetEnumerator();
       }
    }
    


    That being said, I'd also recommend considering switching this to using a List<T> internally.  It makes it easier if you later want to add more cars:

    public class Cars : IEnumerable<Car>
    {
        private List<Car> cars;
        public Cars()
        {
            cars = new List<Car>();
            cars.Add(new Car("FeeFee",200));
            cars.Add(new Car("clunker",90));
            cars.Add(new Car("Fred", 200));
            cars.Add(new Car("zippy", 90));
       }
    
       // Just use the array's enumerator
       public IEnumerator<Car> GetEnumerator()
       {
          return cars.GetEnumerator();
       }
    
       public IEnumerator GetEnumerator()
       {
          return cars.GetEnumerator();
       }
    }
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by sushma rai Friday, September 16, 2011 5:13 PM
    Friday, September 16, 2011 5:06 PM
  • public class Cars : IEnumerable<Car>
    {
        private Car[] carArray;
        public Cars()
        {
            carArray = new Car[4];            //  Car("FeeFee",200);
            carArray[0]=new Car("FeeFee",200);
            carArray[1] = new Car("clunker",90);
            carArray[2] = new Car("Fred", 200);
            carArray[3] = new Car("zippy", 90);
       }
    

    regards, sushma rai http://in.linkedin.com/pub/sushma-rai/28/bb0/988
    Friday, September 16, 2011 5:17 PM
  • we can't use type argument <car> in the above i guess..
    regards, sushma rai http://in.linkedin.com/pub/sushma-rai/28/bb0/988
    Friday, September 16, 2011 5:18 PM
  • we can't use type argument <car> in the above i guess..
    regards, sushma rai http://in.linkedin.com/pub/sushma-rai/28/bb0/988


    Type Array cannot hold an object.

    Type List can hold an object.

    Friday, September 16, 2011 6:37 PM
  • Type Array cannot hold an object.

    Of course, it can.
    Monday, September 19, 2011 7:09 AM