Leaders Squirm Posted September 5, 2005 Leaders Posted September 5, 2005 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: Quote Search the forums | Still IRCing | Be nice
rot13 Posted September 5, 2005 Posted September 5, 2005 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... Quote
Administrators PlausiblyDamp Posted September 5, 2005 Administrators Posted September 5, 2005 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; } } } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders Squirm Posted September 5, 2005 Author Leaders Posted September 5, 2005 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. Quote Search the forums | Still IRCing | Be nice
Diesel Posted September 5, 2005 Posted September 5, 2005 System.Collections.Specialized.StringCollection Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.