Jump to content
Xtreme .Net Talk

Twisting the IEnumerator... how do they do it?


Recommended Posts

  • Leaders
Posted

I noticed that the CharEnumerator class (which implements IEnumerator) is allowed to return a char value for the Current property. Similarly, MessageEnumerator.Current returns a Message object, MethodDataEnumerator.Current returns a MethodData object, and so forth...

 

It seemed to me that the implementation of IEnumerator does not care about the return type of the Current property, since clearly those classes have twisted it to their own needs, and can still claim to implement IEnumerator. Fed up with constantly casting from collections I know to only contain strings, I thought of making my own little wrapper enumerator for string-only collections (although it would work with any object type, since it explicitly calls ToString() on every element):

 


[code=csharp]public class StringEnumerator : IEnumerator
{
//Fields
private IEnumerator innerenum;

//Properties
public string Current
{
get {return this.innerenum.Current.ToString();}
}
//Constructors
public StringEnumerator(IEnumerator enumerator)
{
this.innerenum = enumerator;
}
//Public methods
public bool MoveNext()
{
return this.innerenum.MoveNext();
}
public void Reset()
{
this.innerenum.Reset();
}
}[/code]

 

Does this compile? As you can probably guess, the answer is no, it does not:

 

C:\Documents and Settings\Paul\My Documents\Visual Studio Projects\Toffee\Common\StringEnumerator.cs(66): 'Toffee.StringEnumerator' does not implement interface member 'System.Collections.IEnumerator.Current'. 'Toffee.StringEnumerator.Current' is either static, not public, or has the wrong return type.

 

Yeah, I've had that before, and half expected it. But how do these other classes break the rules?

 

:confused:

Posted

Implement IEnumerator(Of Char)

 

Look in the base types of a class such as CharEnumerator in the object browser and you will see.

 

Works on .NET 2.0 anyways...

  • Administrators
Posted

Not had chance to test if it works but you could explicitly implement the interface

using System;
using System.Collections;

public class StringEnumerator : IEnumerator
{
//Fields
private IEnumerator	innerenum;

//Properties
public string Current
{
	get {return this.innerenum.Current.ToString();}
}
//Constructors
public StringEnumerator(IEnumerator enumerator)
{
	this.innerenum = enumerator;
}
//Public methods
public bool MoveNext()
{
	return this.innerenum.MoveNext();
}
public void Reset()
{
	this.innerenum.Reset();
}

object IEnumerator.Current
{
	get
	{
		// TODO:  Add StringEnumerator.Current getter implementation
		return null;
	}
}
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

Yep, thats what I ended up doing. I installed .Net 2.0, but I'm still using VS.Net 2002 and the whole IEnumerator(Of Char) thing wasn't happening. I found an article (bing!) on MSDN and in Example 2 they used this explicit implementation technique which seems to work.

 

public class StringEnumerator : IEnumerator
{
//Fields
private IEnumerator	innerenum;

//Properties
public string Current
{
	get {return this.innerenum.Current.ToString();}
}
object IEnumerator.Current
{
	get {return this.innerenum.Current;}
}

//Constructors
public StringEnumerator(IEnumerator enumerator)
{
	this.innerenum = enumerator;
}

//Public methods
public bool MoveNext()
{
	return this.innerenum.MoveNext();
}
public void Reset()
{
	this.innerenum.Reset();
}
}

 

Thanks very much.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...