473,326 Members | 2,173 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,326 software developers and data experts.

problem using IEnumerable<T>

jcc

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
'System.Collections.IEnumerable.GetEnumerator()'.
'HelloWorld.A.GetEnumerator()' is either static, not public, or has the
wrong return type.

class A : IEnumerable<string>
{
string[] items = new string[100];

public IEnumerator<string> GetEnumerator()
{
foreach (string s in items)
yield return s;
}
}

Could you tell me what's wrong? It does not compile even if I copy the
example code from Programming C#, 4th Edition, although PC#'s example
is different with mine.

Thanks!

Nov 17 '05 #1
10 2722
KH
Did you read the error message? It says you didn't implement an interface
member, which you didn't... you implemented
System.Collections.Generic.IEnumerable<string>.Get Enumerator(), but you
didn't implement System.Collections.IEnumerable.GetEnumerator().

You'll probably want an explicit interface implementation, so add this to
your class:

System.Collections.IEnumerable System.Collections.IEnumerable.GetEnumerator()
{
return null;
}
"jcc" wrote:

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
'System.Collections.IEnumerable.GetEnumerator()'.
'HelloWorld.A.GetEnumerator()' is either static, not public, or has the
wrong return type.

class A : IEnumerable<string>
{
string[] items = new string[100];

public IEnumerator<string> GetEnumerator()
{
foreach (string s in items)
yield return s;
}
}

Could you tell me what's wrong? It does not compile even if I copy the
example code from Programming C#, 4th Edition, although PC#'s example
is different with mine.

Thanks!

Nov 17 '05 #2
jcc
don't think ur answer is correct. I did implement GetEnumerator(). it's
public, not static, and I can't find the return type is wrong.

The following code is copied from programming c# 4th edition, VS 2005
did not compile it either.

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Enumerable
{
public class ListBoxTest : IEnumerable<String>
{
private string[] strings;
private int ctr = 0;
// Enumerable classes can return an enumerator
public IEnumerator<string> GetEnumerator( )
{
foreach ( string s in strings )
{
yield return s;
}
}

// initialize the list box with strings
public ListBoxTest( params string[] initialStrings )
{
// allocate space for the strings
strings = new String[8];

// copy the strings passed in to the constructor
foreach ( string s in initialStrings )
{
strings[ctr++] = s;
}
}

// add a single string to the end of the list box
public void Add( string theString )
{
strings[ctr] = theString;
ctr++;
}

// allow array-like access
public string this[int index]
{
get
{
if ( index < 0 || index >= strings.Length )
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

// publish how many strings you hold
public int GetNumEntries( )
{
return ctr;
}
}

public class Tester
{
static void Main( )
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest( "Hello", "World" );

// add a few strings
lbt.Add( "Who" );
lbt.Add( "Is" );
lbt.Add( "John" );
lbt.Add( "Galt" );

// test the access
string subst = "Universe";
lbt[1] = subst;

// access all the strings
foreach ( string s in lbt )
{
Console.WriteLine( "Value: {0}", s );
}
}
}
}

Nov 17 '05 #3
KH
> don't think ur answer is correct. I did implement GetEnumerator().

My previous answer is correct; did you try doing what I said? Listen closely:

You implemented System.Collections.Generic.IEnumerable<string>

You DID NOT implement System.Collections.IEnumerable

They are NOT the same thing. This is what the compiler is complaining about.

"jcc" wrote:
don't think ur answer is correct. I did implement GetEnumerator(). it's
public, not static, and I can't find the return type is wrong.

The following code is copied from programming c# 4th edition, VS 2005
did not compile it either.

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Enumerable
{
public class ListBoxTest : IEnumerable<String>
{
private string[] strings;
private int ctr = 0;
// Enumerable classes can return an enumerator
public IEnumerator<string> GetEnumerator( )
{
foreach ( string s in strings )
{
yield return s;
}
}

// initialize the list box with strings
public ListBoxTest( params string[] initialStrings )
{
// allocate space for the strings
strings = new String[8];

// copy the strings passed in to the constructor
foreach ( string s in initialStrings )
{
strings[ctr++] = s;
}
}

// add a single string to the end of the list box
public void Add( string theString )
{
strings[ctr] = theString;
ctr++;
}

// allow array-like access
public string this[int index]
{
get
{
if ( index < 0 || index >= strings.Length )
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

// publish how many strings you hold
public int GetNumEntries( )
{
return ctr;
}
}

public class Tester
{
static void Main( )
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest( "Hello", "World" );

// add a few strings
lbt.Add( "Who" );
lbt.Add( "Is" );
lbt.Add( "John" );
lbt.Add( "Galt" );

// test the access
string subst = "Universe";
lbt[1] = subst;

// access all the strings
foreach ( string s in lbt )
{
Console.WriteLine( "Value: {0}", s );
}
}
}
}

Nov 17 '05 #4

"KH" <KH@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.com...
don't think ur answer is correct. I did implement GetEnumerator().


My previous answer is correct; did you try doing what I said? Listen
closely:

You implemented System.Collections.Generic.IEnumerable<string>

You DID NOT implement System.Collections.IEnumerable

They are NOT the same thing. This is what the compiler is complaining
about.


But

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

is also wrong. This will break (at runtime) any class which tries to
enumerate this collection through its System.Collections.IEnumerable
interface.
Instead you should just return IEnumerable<string>.GetEnumerator() since
IEnumerator<string> implements IEnumerator.
System.Collections.IEnumerable
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

Here's the complete sample.

David
#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Enumerable
{
public class ListBoxTest : IEnumerable<String>,IEnumerable
{
private string[] strings;
private int ctr = 0;
// Enumerable classes can return an enumerator
public IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
{
yield return s;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
// initialize the list box with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[8];

// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

// add a single string to the end of the list box
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}

// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}

}

public class Tester
{
static void Main()
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");

// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");

// test the access
string subst = "Universe";
lbt[1] = subst;

// access all the strings
foreach (object s in lbt)
{
Console.WriteLine("Value: {0}", s);
}
}
}
}
Nov 17 '05 #5
KH
> is also wrong. This will break (at runtime) any class which tries to
enumerate this collection through its System.Collections.IEnumerable
interface.
No it won't - it's an explicit interface implementation. Look it up in the
SDK; it's private to the class.

But it is wrong, it should return an IEnumerator, not an IEnumerable.

"David Browne" wrote:

"KH" <KH@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.com...
don't think ur answer is correct. I did implement GetEnumerator().


My previous answer is correct; did you try doing what I said? Listen
closely:

You implemented System.Collections.Generic.IEnumerable<string>

You DID NOT implement System.Collections.IEnumerable

They are NOT the same thing. This is what the compiler is complaining
about.


But

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

is also wrong. This will break (at runtime) any class which tries to
enumerate this collection through its System.Collections.IEnumerable
interface.
Instead you should just return IEnumerable<string>.GetEnumerator() since
IEnumerator<string> implements IEnumerator.
System.Collections.IEnumerable
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

Here's the complete sample.

David
#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Enumerable
{
public class ListBoxTest : IEnumerable<String>,IEnumerable
{
private string[] strings;
private int ctr = 0;
// Enumerable classes can return an enumerator
public IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
{
yield return s;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
// initialize the list box with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[8];

// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

// add a single string to the end of the list box
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}

// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}

}

public class Tester
{
static void Main()
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");

// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");

// test the access
string subst = "Universe";
lbt[1] = subst;

// access all the strings
foreach (object s in lbt)
{
Console.WriteLine("Value: {0}", s);
}
}
}
}

Nov 17 '05 #6

"KH" <KH@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
is also wrong. This will break (at runtime) any class which tries to
enumerate this collection through its System.Collections.IEnumerable
interface.


No it won't - it's an explicit interface implementation. Look it up in the
SDK; it's private to the class.


The declaration is fine. You just aren't supposed to
return null;
You need to return a valid System.Collections.IEnumerable. Not null.

You'll notice that the exaple I posted also used explicit interface
implementation.

David
Nov 17 '05 #7
KH
Indeed yours does, sorry I didn't read closely. But the explicit
implementation is not visible to users of the class; you could use it
internally, but why would you do that? It's 6 one way 1/2 dozen the other.
"David Browne" wrote:

"KH" <KH@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
is also wrong. This will break (at runtime) any class which tries to
enumerate this collection through its System.Collections.IEnumerable
interface.


No it won't - it's an explicit interface implementation. Look it up in the
SDK; it's private to the class.


The declaration is fine. You just aren't supposed to
return null;
You need to return a valid System.Collections.IEnumerable. Not null.

You'll notice that the exaple I posted also used explicit interface
implementation.

David

Nov 17 '05 #8

"KH" <KH@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
Indeed yours does, sorry I didn't read closely. But the explicit
implementation is not visible to users of the class; you could use it
internally, but why would you do that? It's 6 one way 1/2 dozen the other.


No. An explict interface implementation is public, but only visible after a
cast to the interface.

If a user of the class had cast it to IEnumerable, or (more likely) passed
it to a method which expected IEnumerable then the explicitly implemented
interface method would be visible.

EG

passing the class to this function

void EnumMe(IEnumerable col)
{
foreach (object o in col)
{
Console.WriteLine(o);
}
}

would have resulted in an exception.

David
Nov 17 '05 #9
KH <KH@discussions.microsoft.com> wrote:
Indeed yours does, sorry I didn't read closely. But the explicit
implementation is not visible to users of the class; you could use it
internally, but why would you do that? It's 6 one way 1/2 dozen the other.


You've missed the point of explicit interface implementation. It's
*not* private to the class - it's used by anything which only knows
about the object as an IEnumerable. If I do:

IEnumerable foo = new ListBoxTest();

IEnumerator x = foo.GetEnumerator();

that would return null, which is definitely the *wrong* thing to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
"jcc" <jc****@gmail.com> writes:
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
'System.Collections.IEnumerable.GetEnumerator()'.
'HelloWorld.A.GetEnumerator()' is either static, not public, or has the
wrong return type.


See

http://www.dina.kvl.dk/~sestoft/csha...ly/errata.html

under Section 24.2.2

Peter
Nov 17 '05 #11

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

Similar topics

6
by: Doug Dew | last post by:
This won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; namespace MyNamespace { public class MyClass<T> : IEnumerable<T> { // Appropriate stuff here }
0
by: clécio | last post by:
Hi folks! i developed a web custom control inherited from webcontrol, but when i try to bound a property using <%= %>, i have all the code put between de ' ! ex.: <uc1:SkinControlManager...
1
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is...
3
by: ruskie | last post by:
I created a user control with two public properties and drop it to an aspx page as the following <uc1:myUserControl id="myUserControl1" runat="server" MyProperty1="<%= this.MyVar1 %>"...
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: 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: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs & functions have to be in <scripttags. Thanks! ...
0
by: Marc Gravell | last post by:
You could bypass the List<T>'s tendency to use ICollection<Tby simply not giving it an ICollection<T- for example, with the C# 3 extension method below you could use: List<Tlist = new...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.