why compiler error re: method GetEnumerator() not in context

  • Thread starter Thread starter Nu2Csharp
  • Start date Start date
N

Nu2Csharp

Guest
I typed the following code verbatim from the documents at IEnumberator but I get a compiler error at line 40 stating "Name GetEnumerator does not exist in the current context". I have seen this error when I forget to extend the right interface, and I think I have done that. Can someone have a look and see if they can figure out why I get this error?
using System;
using System.Collections;

namespace EnumeratorExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
public class People : IEnumerable
{
private Person[] _people;

People(Person[] pArray)
{
//I am going to try this and see what it gives me
//this._people = pArray;

_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people = pArray;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
}
public PeopleEnum GetEnumertor()
{
return new PeopleEnum(_people);
}
//When you implement IEnumerable you must also implement IEnumerator

}
public class PeopleEnum: IEnumerator
{
public Person[] _people;

int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
object IEnumerator.Current {
get
{
return Current;
}
}
public Person Current
{
get
{
try
{
return _people[position];
}
catch(IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}

public bool MoveNext()
{
position++;
return (position < _people.Length);
}

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

}

Continue reading...
 
Back
Top