473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

LINQ implementing IEnumerable<T>, IEnumerator<T>

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<CompanyGetEnumerator() {
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;
}
}

}
}
Dec 9 '06 #1
5 9361
"IEnumerator<Company>.Current" will be satisfied by "public Company
Current"; you mean to explicitely implement the *non-generic*
IEnumerator interface;

object IEnumerator.Current {...}

Marc

Dec 9 '06 #2
Hi,

Instead of deriving from CollectionBase, derive from
System.Collections.ObjectModel.Collection<Companya nd forget about a custom
IEnumerator implementation.

--
Dave Sexton

"Shikari Shambu" <sh*************@hotmail.comwrote in message
news:O6**************@TK2MSFTNGP04.phx.gbl...
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<CompanyGetEnumerator() {
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;
}
}

}
}

Dec 9 '06 #3
Or indeed limit your custom enumerators to 2.0 "yield" style ones ;-p

But the base-class is good too

Marc

Dec 10 '06 #4
Hi Marc,

Yea, I like writing custom iterators using yield, but in this case
Collection<Twould 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 :)

--
Dave Sexton

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
Or indeed limit your custom enumerators to 2.0 "yield" style ones ;-p

But the base-class is good too

Marc

Dec 10 '06 #5
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

Dec 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
3
by: Adam Clauss | last post by:
I am developing an abstract class which implements IEnumerable<T>. I need the actual implemented methods here to be abstract as well - they will be implemented by MY subclasses. However, I...
1
by: nick_tucker | last post by:
Hi, Could someone tell me which of the following is correct if I want to make the MyCollection class below enumerable public class MyCollection:System.Collections.IEnumerable { private...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
2
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return",...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
2
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... ...
0
by: Andrus | last post by:
I noticed that Linq-SQL DataContext ExecuteQuery method does not have new() constraint: IEnumerable<TResultExecuteQuery( command, ... ) IEnumerable must return object so it must create this...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.