CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2011
    Posts
    1

    Unhappy Most confusing error I have ever encountered

    Hi all.
    I am trying to make an app for the .NET framework, and am going well, except that when I tried to implement IEnumerable on a custom object I get the following error:

    "Connot implicitly convert type 'System.Collections.Generic.IEnumerator<MyClass>' to 'System.Collections.Generic.IEnumerator<MyClass>'. An explicit conversion exists (are you missing a cast?)"

    It might just be me, but that does not make any sense what-so-ever.

    Context:

    public class MyClass...
    public class MyClassCollection
    {
    private List<MyClass> InnerList;
    public IEnumerable<MyClass> GetEnumerator()
    {
    return ((IEnumerable<MyClass>)this.InnerList).GetEnumerator(); // Line that has error
    }
    }

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Most confusing error I have ever encountered

    Are you sure that what you've posted is in fact the error you've got? Maybe you mistyped something (IEnumerator/IEnumerable)?

    Quote Originally Posted by LEGEND383 View Post
    public class MyClass...
    public class MyClassCollection
    {
    private List<MyClass> InnerList;
    public IEnumerable<MyClass> GetEnumerator()
    {
    return ((IEnumerable<MyClass>)this.InnerList).GetEnumerator(); // Line that has error
    }
    }
    Why does your GetEnumerator() method return an IEnumerable?
    Fix all that, and the error should be gone. Also, I think that you can return an enumerator by just calling this.InnerList.GetEnumerator().

    P.S. Please use the [code][/code] tags when posting code - it will preserve formatting.
    Last edited by TheGreatCthulhu; May 11th, 2011 at 03:43 PM.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Most confusing error I have ever encountered

    Yes, and you also don't need the cast anyway. Just return InnerList.GetEnumerator()

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured