Click here to Skip to main content
15,041,393 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharp_p332_CustomGenericCollection
{
    public class CarCollection<T> : IEnumerable<T>
    {
        private List<T> arCars = new List<T>();

        public T GetCar(int pos)
        {
            return arCars[pos];
        }

        public void AddCar(T c)
        {
            arCars.Add(c);
        }

        public void ClearCars()
        {
            arCars.Clear();
        }

        public int Count
        {
            get { return arCars.Count; }
        }

        // IEnumerable<T> extends IEnumerable, therefore we need to implement both versions of GetEnumerator()
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return arCars.GetEnumerator();
        }
    }
}



This is the class

CarCollection.cs

If someone could tell me whats wrong with it , that would be great..thanks
Posted
Updated 26-Oct-11 5:40am
v5
Comments
BobJanova 26-Oct-11 11:13am
   
What is the question? What are you trying to do? Why don't you just use a List<Car>?
[no name] 26-Oct-11 11:28am
   
Because his teacher said they have to impliment IEnermerable<t> :-)
BillWoodruff 26-Oct-11 20:52pm
   
Indeed, the question that 'jumps out' reading the code is why isn't this just a dedicated List<car> ... or, if the future goal is to have various instances differentiated by some attribute of Cars ... List<Renault>, List<Edsel>, etc. ... why not a virtual interface that the instances would inherit/implement ?
BillWoodruff 26-Oct-11 20:57pm
   
It's always useful to me before creating some Generic entity to review the options:http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

To use an interface you must impliment its members. You can auto generate the template by right clicking on the interface and telling the IDE to impliment it for you.

You will then get property and method stubs that will throw exceptions if accessed. You will then need to actually impliment the interface.

You may want to look over MSDN of How to create an iterator block for generic list[^]
   
Comments
BobJanova 26-Oct-11 11:43am
   
Good link. Using yield return feels a lot like cheating, heh.
Well you could take Bob's advice, or add the implementation of the IEnumerable interface as required by your class definition.
   
If the collection is a collection of cars, why won't you just use
C#
public class CarCollection : IEnumerable

Anyway, IEnumerable requires that you would have an IEnumerator
It should be something like this... I didn't compile it, so I'm not sure that it actually compiles good, but its about right I think.
C#
public class CarCollection : IEnumerable
{

        List<car> cars;

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)GetEnumerator();
        }

        public CarEnum GetEnumerator()
        {
            return new CarEnum(cars);
        }

        public class CarEnum : IEnumerator
        {
            private List<car> cars;

            int position = -1;

            public CarEnum(List<car> commands)
            {
                this.cars = cars;
            }

            public bool MoveNext()
            {
                position++;
                return (position < cars.Count);
            }

            public void Reset()
            {
                position = -1;
            }

            object IEnumerator.Current
            {
                get
                {
                    return Current;
                }
            }

            public Car Current
            {
                get
                {
                    try
                    {
                        return cars[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }

        /* You can add more methods of course... */
}

</car></car></car>
   
v2
Comments
BobJanova 26-Oct-11 11:38am
   
The internals of an enumerator should be private, particularly the list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900