473,326 Members | 2,113 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.

Implementing a Enumerator interface

Hi ,
I am learning C# and now am stuck with a simple prorgam.Tried googling
but didn't get an answer :(. The following program gives me three
compilation errors.Can anyone enlighten me? Thanks.

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

namespace Collections
{
public class Tokens : IEnumerable<String>
{
private String[] elements;

Tokens(String source, char[] delimiters)
{
elements = source.Split(delimiters);
}

~Tokens()
{
}

public IEnumerator<StringGetEnumerator()
{
return new TokenEnumerator(this);
}

private class TokenEnumerator:IEnumerator<String>
{
private Int32 pos = -1;
private Tokens t;

public TokenEnumerator(Tokens tok)
{
t = tok;
}

public Boolean MoveNext()
{
if (pos < t.elements.Length - 1)
{
pos++;
return true;
}
else
{
return false;
}
}

public void Reset()
{
pos = -1;
}

public String Current
{
get { return t.elements[pos]; }
}
}

public static void Main()
{
Tokens f = new Tokens("This is my first C# program", new
char[] { ' ', '#' });
foreach (String s in f)
{
Console.WriteLine(s);
}
}
}
}

Error 1 'Collections.Tokens.TokenEnumerator' does not implement
interface member 'System.IDisposable.Dispose()'
Error 2 'Collections.Tokens.TokenEnumerator' does not implement
interface member 'System.Collections.IEnumerator.Current'.
'Collections.Tokens.TokenEnumerator.Current' is either static, not
public, or has the wrong return type

Error 3 'Collections.Tokens' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Collections.Tokens.GetEnumerator()' is either static, not public, or
has the wrong return type.

Sep 3 '06 #1
3 8891
Senthil wrote:
Hi ,
I am learning C# and now am stuck with a simple prorgam.Tried googling
but didn't get an answer :(. The following program gives me three
compilation errors.Can anyone enlighten me? Thanks.
First, since you're using C# 2.0, it's much (much, much!) easier to use the
new interator syntax that's built into the language (look up 'yield return'
in the C# language reference). See additional comments about the errors
you're getting inline, below.
>
using System;
Add:

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

namespace Collections
{
public class Tokens : IEnumerable<String>
{
private String[] elements;

Tokens(String source, char[] delimiters)
{
elements = source.Split(delimiters);
}

~Tokens()
{
}

public IEnumerator<StringGetEnumerator()
{
return new TokenEnumerator(this);
}
You also need

IEnumerator IEnumerable.GetEnumerator()
{
return new TokenEnumerator(this);
}

to supply an implementation for IEnumerable.GetEnumerator, since
IEnumerable<Tderives from IEnumerable.
>
private class TokenEnumerator:IEnumerator<String>
{
private Int32 pos = -1;
private Tokens t;

public TokenEnumerator(Tokens tok)
{
t = tok;
}

public Boolean MoveNext()
{
if (pos < t.elements.Length - 1)
{
pos++;
return true;
}
else
{
return false;
}
}

public void Reset()
{
pos = -1;
}

public String Current
{
get { return t.elements[pos]; }
}
This supplies IEnumerator<string>.Current, but you also need

object IEnumerator.Current
{
get {re turn t.elements[pos]; }
}

to supply an implementation of IEnumerator.Current since IEnumerator<sT>
derives from IEnumerator.
You also need

void IDisposable.Dispose()
{
}

since IEnumerator<Tderives from IDisposable.
}

public static void Main()
{
Tokens f = new Tokens("This is my first C# program", new
char[] { ' ', '#' });
foreach (String s in f)
{
Console.WriteLine(s);
}
}
}
}

Sep 3 '06 #2
Senthil wrote:
I am learning C#
I see you are using C# 2.0.
and now am stuck with a simple prorgam.Tried googling
but didn't get an answer :(. The following program gives me three
compilation errors.Can anyone enlighten me? Thanks.
I'll go through the errors first, before I deal with the code.
Error 1 'Collections.Tokens.TokenEnumerator' does not implement
interface member 'System.IDisposable.Dispose()'
This refers to the fact that IEnumerator<Tdescends from IDisposable,
so to implement IEnumerator<T>, you also need to implement IDisposable.
Error 2 'Collections.Tokens.TokenEnumerator' does not implement
interface member 'System.Collections.IEnumerator.Current'.
'Collections.Tokens.TokenEnumerator.Current' is either static, not
public, or has the wrong return type
This refers to the fact that IEnumerator<Talso descends from
IEnumerator, and the IEnumerator.Current property is of type 'object',
not 'string'. You'd need an explicit implementation, like this:

object IEnumerator.Current
{
get { return Current; } // uses the Current of type 'string'
}
Error 3 'Collections.Tokens' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Collections.Tokens.GetEnumerator()' is either static, not public, or
has the wrong return type.
This is similar to the error for Current. Again, you need an explicit
implementation:

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

Note that calling methods like this, inside the type, never resolves to
an explicit interface method implementation. So, this won't recurse
indefinitely.
using System;
using System.Collections.Generic;
using System.Text;
[...]
~Tokens()
{
}
This is a finalizer. It is *highly* unlikely that your type needs a
finalizer, and its presence will make your class a lot less efficient
than it can be.

Finally, I'd like to point you to iterators, a feature of C# 2.0 which
make implementing enumerators a lot easier (ignoring the fact that you
could just return 'elements.GetEnumerator()'). Your program could be
written with iterators in the following manner:

---8<---
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Collections
{
public class Tokens : IEnumerable<String>
{
private String[] elements;

Tokens(String source, char[] delimiters)
{
elements = source.Split(delimiters);
}

~Tokens()
{
}

public IEnumerator<StringGetEnumerator()
{
for (int i = 0; i < elements.Length; ++i)
yield return elements[i];
}

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

public static void Main()
{
Tokens f = new Tokens("This is my first C# program", new
char[] { ' ', '#' });
foreach (String s in f)
{
Console.WriteLine(s);
}
}
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Sep 3 '06 #3

Barry Kelly wrote:
Senthil wrote:
I am learning C#

I see you are using C# 2.0.
and now am stuck with a simple prorgam.Tried googling
but didn't get an answer :(. The following program gives me three
compilation errors.Can anyone enlighten me? Thanks.

I'll go through the errors first, before I deal with the code.
Error 1 'Collections.Tokens.TokenEnumerator' does not implement
interface member 'System.IDisposable.Dispose()'

This refers to the fact that IEnumerator<Tdescends from IDisposable,
so to implement IEnumerator<T>, you also need to implement IDisposable.
Error 2 'Collections.Tokens.TokenEnumerator' does not implement
interface member 'System.Collections.IEnumerator.Current'.
'Collections.Tokens.TokenEnumerator.Current' is either static, not
public, or has the wrong return type

This refers to the fact that IEnumerator<Talso descends from
IEnumerator, and the IEnumerator.Current property is of type 'object',
not 'string'. You'd need an explicit implementation, like this:

object IEnumerator.Current
{
get { return Current; } // uses the Current of type 'string'
}
Error 3 'Collections.Tokens' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Collections.Tokens.GetEnumerator()' is either static, not public, or
has the wrong return type.

This is similar to the error for Current. Again, you need an explicit
implementation:

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

Note that calling methods like this, inside the type, never resolves to
an explicit interface method implementation. So, this won't recurse
indefinitely.
using System;
using System.Collections.Generic;
using System.Text;
[...]
~Tokens()
{
}
This is a finalizer. It is *highly* unlikely that your type needs a
finalizer, and its presence will make your class a lot less efficient
than it can be.

Finally, I'd like to point you to iterators, a feature of C# 2.0 which
make implementing enumerators a lot easier (ignoring the fact that you
could just return 'elements.GetEnumerator()'). Your program could be
written with iterators in the following manner:

---8<---
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Collections
{
public class Tokens : IEnumerable<String>
{
private String[] elements;

Tokens(String source, char[] delimiters)
{
elements = source.Split(delimiters);
}

~Tokens()
{
}

public IEnumerator<StringGetEnumerator()
{
for (int i = 0; i < elements.Length; ++i)
yield return elements[i];
}

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

public static void Main()
{
Tokens f = new Tokens("This is my first C# program", new
char[] { ' ', '#' });
foreach (String s in f)
{
Console.WriteLine(s);
}
}
}
}
--->8---

-- Barry
Thanks for your replies folks!!!
>
--
http://barrkel.blogspot.com/
Sep 3 '06 #4

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

Similar topics

6
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick questions: 1. My understanding of UML says that...
2
by: Gordon Rundle | last post by:
It drives me nuts that I can't use foreach with an enumerator instance. I would like the following to be functionally identical: foreach (Object o in MyCollection) ... foreach (Object o in...
2
by: Pavils Jurjans | last post by:
Hello, please consider the code at the end of this posting, that depicts simple case of custom enumerator. This particular example will enumerate over list of random number within 1..10, and will...
4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
18
by: Rob Panosh | last post by:
Hello, When traversing an ArrayList which is faster? For Each oItem as Something in me.ArrayList ..... ..... Next OR
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...
7
by: csharpula csharp | last post by:
Hello, I have methods which refer to some class enumerator .Should I make it pulic or encapsulate it? *** Sent via Developersdex http://www.developersdex.com ***
6
by: Raj Wall | last post by:
Hi, I am trying to implement the IEqualityComparer interface for a struct so I can use it as the Key for a Dictionary. My struct declaration has: public struct Ring : IEqualityComparer {...
5
by: Nikolay Belyh | last post by:
I have created a "collection" in C# like this: namespace ClassLibrary1 { public class X {} public class Class1 { public ArrayList objs {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: 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

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.