Beginner Implement IList problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    #1

    Beginner Implement IList problem

    Hi all,

    I'm trying to implement IList and keep getting an error when trying to
    implement GetEnumerator() .
    My class has a List<String> and I've been using its methods as return
    types for IList, but I can't seem to figure the the get enumerator
    section. I try:

    //IEnumerable
    public IEnumerator GetEnumerator()
    {

    return list.GetEnumera tor();
    }

    but keep getting the error:

    Error 1 'Foo' does not implement interface member
    'System.Collect ions.Generic.IE numerable<strin g>.GetEnumerato r()'.
    Foo.GetEnumerat or()' is either static, not public, or has the wrong
    return type.

    Any help would be appreciated.

    Thanks,

    Paul

  • Jon Skeet [C# MVP]

    #2
    Re: Beginner Implement IList problem

    Paul <Gef.Mongoose@g mail.com> wrote:[color=blue]
    > I'm trying to implement IList and keep getting an error when trying to
    > implement GetEnumerator() .
    > My class has a List<String> and I've been using its methods as return
    > types for IList, but I can't seem to figure the the get enumerator
    > section. I try:
    >
    > //IEnumerable
    > public IEnumerator GetEnumerator()
    > {
    >
    > return list.GetEnumera tor();
    > }
    >
    > but keep getting the error:
    >
    > Error 1 'Foo' does not implement interface member
    > 'System.Collect ions.Generic.IE numerable<strin g>.GetEnumerato r()'.
    > Foo.GetEnumerat or()' is either static, not public, or has the wrong
    > return type.[/color]

    Are you sure you're trying to implement IList rather than
    IList<string>? The error message suggests that you're trying the latter
    (which itself derives from IEnumerable<T>) whereas your method
    declaration is really implementing IEnumerable (which you need if
    you're implementing the non-generic IList). In fact, you'll need the
    non-generic version anyway, because IEnumerable<T> extends IEnumerable.
    You'll need to implement one of them explicitly, eg:

    public IEnumerator<str ing> GetEnumerator()
    {
    return list.GetEnumera tor();
    }

    public IEnumerator IEnumerable.Get Enumerator()
    {
    return GetEnumerator() ;
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    Working...