tags:

views:

99

answers:

5

Hello All,

I trying to write an application that has a collection of students using different types of collections (List, ArrayList, Dictionary)

Requirements:

  1. Use C# Iterators (not FOREACH)
  2. Return IEnumerable of Students

I created a class called Students implementing IEnumerable interface

I have 3 methods for each collection and each returning an IEnunerable of Students

 public class Students : IEnumerable
    {

        //Generic List
        public IEnumerable getList()
        {
            List<string> ListStudents = new List<string>();
            ListStudents.Add("Bob");
            ListStudents.Add("Nancy");
            for (int i = 0; i < ListStudents.Count; i++)
            {
                yield return ListStudents[i];
            }
        }

        //Generic Dictionary
        public IEnumerable getDiction()
        {
            Dictionary<string, string> DicStudents = new Dictionary<string, string>();

            DicStudents.Add("FirstName", "Nana");


            for (int i = 0; i < DicStudents.Count; i++)
            {
                yield return DicStudents.Values.ToList()[i];
            }
        }

        //Array List
        public IEnumerable getArray()
        {
            ArrayList ArrayStudents = new ArrayList { "Tom", "Noah", "Peter" };

            for (int i = 0; i < ArrayStudents.Count; i++)
            {
                yield return ArrayStudents[i];
            }
        }

        public IEnumerator GetEnumerator()
        {


        }

    }

How do I take the 3 collections above and iterate through them as if there were one collection. At the moment, I am putting them into an Array but I cannot seem to iterate through them:

   public class GetALL: IEnumerable
    {
        public IEnumerator GetEnumerator()
        {

            Students s = new Students();

            IEnumerator[] enumerator = new IEnumerator[3] 
            {
                s.getList().GetEnumerator(),
                s.getDiction().GetEnumerator(),
                s.getArray().GetEnumerator(),
            };
             return enumerator[3];
        }
    }

There must be an easier way or a way that it can actually be donw....

Thanks

:)


Ok I misunderstood the request.

I have the below:

public class Student : IEnumerable
    {
        int ID;
        string Name;
        string Phone;


        public IEnumerable<Student> getStudentList()
        {
            List<Student> e = new List<Student>();
            e.Add(new Student() { Name = "Bob", ID = 20, Phone = "914-123-1234" });
            e.Add(new Student() { Name = "Jack", ID = 21, Phone = "718-123-1234" });
            e.Add(new Student() { Name = "Nancy", ID = 22, Phone = "212-123-1234" });

            for (int i = 0; i < e.Count; i++)
            {

                yield return e[i];

            }
        }
        public IEnumerator GetEnumerator()
        {

        }

    }

How do I return the List of Students and write them to the console? I want to keep my list of students in the Student Object and then iterate through them object and print the list of students?

Is this possible?

+2  A: 

You can make three for loops over the three lists and yield return inside each one

SLaks
A: 

Refactor your GetALL class to call the child enumerators in sequence so when the first MoveNext() returns false the parent class would move to the next child enumerator. In this approach the parent class just acts as a metaenumerator bridging the children together.

Tedford
+1  A: 

What exactly is it that you are trying to return? That is, when you say "MoveNext", what type of object will be the Current? You seems to be saving different things in the different lists.

Also, forget you ever heard of ArrayList. ArrayList is a leftover from .Net v1.1, when it didn't have generics. You are far better off using a type-specific List<>. The only place it is at all useful is when you need a list of different objects, with no common base class. However, that's usually a sign of a bad design, and would probably be better in an List<Object> anyway.

James Curran
+1 For the last sentence.
driis
lol, so true. so true.
maxwellb
I just edited the above. Hopefully it makes more sense.
Mage
Btw, why the comment about ArrayList? Just curious...
Mage
@Mage: I've expanded my comment on ArrayList above.
James Curran
A: 

Consider a related question:

You have a number N of baskets of Fruit, each with some number of fruits in it.

You could call the number of fruits in basket n something like M. Remember, M will be different for each basket #n.

Can you see that there are indeed two levels of iteration? There are two variables, like an X and a Y axis.

To approach this problem, you would first in an outer loop, iterate over the baskets. Then, inside the loop you are working with one, fixed basket. From there, you can set up an inner loop, to iterate over the individual fruits in the current basket.

maxwellb
A: 

OK, now your Student class is really messed up. AS it is defined, each Student has a list of Students within it.

You will be needing three classes here:

  • A Student class - Keep the data members (which should be Properties) and get rid of the rest.
  • A Students class - Which you will probably want do implement IList (and probably derived from List

  • A Program class, which will contain your Main() method, and will hold you Students object, and will have the method responsible for putting the Student objects into the Students object.

Also, note that a List is also an IEnumerable, so your code:

        List<Student> e = new List<Student>();   
        //// blah-blah-blah   
        for (int i = 0; i < e.Count; i++)   
        {   
            yield return e[i];   
        }

Is exactly the same as:

        List<Student> e = new List<Student>();   
        //// blah-blah-blah 
       return e;  
James Curran