473,395 Members | 1,464 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,395 software developers and data experts.

Help with IEnumerable / IEnumerator

I have been trying to get the IEnumerable interface to compile, and am
having some difficulties.

When I try to compile:

class TestIEnum : IEnumerable<string{

string[] theStrings;

public IEnumerator<stringGetEnumerator() {
foreach (string s in theStrings) {
yield return s;
}
}
} // end class TestIEnum

I get the error message: "'TestIEnum' does not implement interface
member 'System.Collections.IEnumerable.GetEnumerator()'.
'TestIEnum.GetEnumerator()' is either static, not public, or has the
wrong return type."

I can see that GetEnumerator() is public and not static, so I assume
my error is in the return type. I have tried both string and the
non-generic IEnumerator as return types, neither works.

I noticed that the error message refers to
System.Collections.IEnumerable.GetEnumerator(), the non-generic
version. So I tried fully qualifying the name of GetEnumerator(),
again no joy.

What am I missing here?

Thanks in advance,

rossum
Nov 26 '06 #1
3 3575
rossum <ro******@coldmail.comwrote:
>I noticed that the error message refers to
System.Collections.IEnumerable.GetEnumerator(), the non-generic
version. So I tried fully qualifying the name of GetEnumerator(),
again no joy.
You have to both implement the generics version (as you did), and also
explicitly implement the old version. e.g.

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{ return GetEnumerator();
}

--
Lucian
Nov 26 '06 #2
Hi,

You are missing the non-generic version. IEnumerable<Timplements
IEnumerable.

Both must be implemented, but commonly the non-generic version is
implemented explicitly and just calls the generic version:

class TestIEnum : IEnumerable<string>
{
public IEnumerator<stringGetEnumerator()
{
...
}

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

--
Dave Sexton

"rossum" <ro******@coldmail.comwrote in message
news:q8********************************@4ax.com...
>I have been trying to get the IEnumerable interface to compile, and am
having some difficulties.

When I try to compile:

class TestIEnum : IEnumerable<string{

string[] theStrings;

public IEnumerator<stringGetEnumerator() {
foreach (string s in theStrings) {
yield return s;
}
}
} // end class TestIEnum

I get the error message: "'TestIEnum' does not implement interface
member 'System.Collections.IEnumerable.GetEnumerator()'.
'TestIEnum.GetEnumerator()' is either static, not public, or has the
wrong return type."

I can see that GetEnumerator() is public and not static, so I assume
my error is in the return type. I have tried both string and the
non-generic IEnumerator as return types, neither works.

I noticed that the error message refers to
System.Collections.IEnumerable.GetEnumerator(), the non-generic
version. So I tried fully qualifying the name of GetEnumerator(),
again no joy.

What am I missing here?

Thanks in advance,

rossum


Nov 26 '06 #3
On Sun, 26 Nov 2006 00:30:44 +0000, rossum <ro******@coldmail.com>
wrote:
>I have been trying to get the IEnumerable interface to compile, and am
having some difficulties.

When I try to compile:

class TestIEnum : IEnumerable<string{

string[] theStrings;

public IEnumerator<stringGetEnumerator() {
foreach (string s in theStrings) {
yield return s;
}
}
} // end class TestIEnum

I get the error message: "'TestIEnum' does not implement interface
member 'System.Collections.IEnumerable.GetEnumerator()'.
'TestIEnum.GetEnumerator()' is either static, not public, or has the
wrong return type."

I can see that GetEnumerator() is public and not static, so I assume
my error is in the return type. I have tried both string and the
non-generic IEnumerator as return types, neither works.

I noticed that the error message refers to
System.Collections.IEnumerable.GetEnumerator(), the non-generic
version. So I tried fully qualifying the name of GetEnumerator(),
again no joy.

What am I missing here?

Thanks in advance,

rossum
Thanks Dave and Lucian, that worked fine.

rossum

Nov 26 '06 #4

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...
5
by: ukjock | last post by:
Hi guys and Gals I need some serious help please. I can not get my head around this problem. At work we have a BBC Acorn computer (yep, I know what yer thinking), and for the past month I have...
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...
6
by: nick | last post by:
I have a function must return an one-row IEnumerable object. Any lesser cost way than the following code? static IEnumerable MyUDF() { DataTable dt; dt.Columns.Add("Test"); dt.Rows.Add(1);...
5
by: Tin Gherdanarra | last post by:
Dear mpdls, here is a simple example of an IEnumerable that generates integers: It works, but I have only a vague idea of what's going on. I understand that /yield/ wraps the humble integer...
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",...
12
by: gnewsgroup | last post by:
I've read the msdn doc about IEnumerable. It seems to me that IEnumerable objects are essentially wrapped-up arrays. It simply gives us the foreach convenience. Is this correct?
4
by: jmDesktop | last post by:
In the code below from MSDN How do the PeopleEnum methods ever get called? foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); What is going on behind the...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.