LINQ implementing IEnumerable<T>, IEnumerator<T>

S

Shikari Shambu

Hi,
I am trying to implement a collection that implements IEnumerable<T>. I
keep getting the following error

'IEnumerator<...>.Current' in explicit interface declaration is not a member
of interface. Please help me resolve the error


I was able to implement the non Generics version

See Code below
public class CompanyCollection: System.Collections.CollectionBase,
IEnumerable<Company>
{

public Company this[int index]
{
get
{
return ((Company)(List[index]));
}
set
{
List[index] = value;
}
}

public bool Contains(Company aCompany)
{
return List.Contains(aCompany);
}

public int Add(Company aCompany)
{
return List.Add(aCompany);
}

public void Insert(int index, Company aCompany)
{
List.Insert(index, aCompany);
}

public void Remove(Company aCompany)
{
List.Remove(aCompany);
}


// Implement the GetEnumerator() method:
public IEnumerator<Company> GetEnumerator() {
return new CompanyEnumerator(this);
}


/* Define the Enumeration class to return */
// Inner class implements IEnumerator interface:
private class CompanyEnumerator : IEnumerator<Company>
{
private int position = -1;
private CompanyCollection coll;

public CompanyEnumerator(CompanyCollection col)
{
this.coll = col;
}

// Declare the MoveNext method required by IEnumerator:
public bool MoveNext()
{
if (position < coll.Count - 1)
{
position++;
return true;
}
else
{
return false;
}
}

// Declare the Reset method required by IEnumerator:
public void Reset()
{
position = -1;
}

// Gets the current element in the collection.
public Company Current {
get {
return((Company)coll[position]);
}
}
// Declare the Current property required by IEnumerator:
object IEnumerator<Company>.Current
{
get
{
return Current;
}
}



}
}
 
M

Marc Gravell

"IEnumerator<Company>.Current" will be satisfied by "public Company
Current"; you mean to explicitely implement the *non-generic*
IEnumerator interface;

object IEnumerator.Current {...}

Marc
 
D

Dave Sexton

Hi,

Instead of deriving from CollectionBase, derive from
System.Collections.ObjectModel.Collection<Company> and forget about a custom
IEnumerator implementation.
 
M

Marc Gravell

Or indeed limit your custom enumerators to 2.0 "yield" style ones ;-p

But the base-class is good too

Marc
 
D

Dave Sexton

Hi Marc,

Yea, I like writing custom iterators using yield, but in this case
Collection<T> would also reduce the OP's CompanyCollection class to
absolutely no implementation at all since each of the members that were
provided in the OP's sample code are provided by the Collection<T>, with the
exact same behavior :)
 
M

Marc Gravell

Agree entirely and 100%. Just "for completeness" in those cases where
the base / encapsulated implementation doesn't quite do it. In 2.0
there are thankfully very few cases where it is necessary to write an
iterator *class*.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top