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;
}
}
}
}
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;
}
}
}
}