Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with IEnumerable / IEnumerator

rossum
Guest
 
Posts: n/a
#1: Nov 26 '06
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



Lucian Wischik
Guest
 
Posts: n/a
#2: Nov 26 '06

re: Help with IEnumerable / IEnumerator


rossum <rossum48@coldmail.comwrote:
Quote:
>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
Dave Sexton
Guest
 
Posts: n/a
#3: Nov 26 '06

re: Help with IEnumerable / IEnumerator


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" <rossum48@coldmail.comwrote in message
news:q8nhm29p7t025e5d7bl7dlgmfn4h33u4pn@4ax.com...
Quote:
>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
>
>

rossum
Guest
 
Posts: n/a
#4: Nov 26 '06

re: Help with IEnumerable / IEnumerator


On Sun, 26 Nov 2006 00:30:44 +0000, rossum <rossum48@coldmail.com>
wrote:
Quote:
>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

Closed Thread