locked
Wrapping a collection into a class - Can't do it !!

    Question

  • Hi,


    What's wrong with my code ?

     

    using System;

    using System.Linq;

    using System.Collections;

    using System.Collections.Generic;

    using System.Collections.ObjectModel;

    using System.IO;

    using System.Windows.Media.Imaging;

    using System.Windows.Data;

    using System.Threading;

    using System.Windows.Threading;

    using System.Xml.Linq;

    using System.ComponentModel;

    using System.Windows;

     

    public class PhotoCategories : IEnumerable<PhotoCategory>

    {

    private ObservableCollection<PhotoCategory> _categories;

    public IEnumerator<PhotoCategory> GetEnumerator()

    {

    foreach (PhotoCategory photoCat in _categories)

    {

    yield return photoCat;

    }

    }

    // Constructor

    public PhotoCategories ()

    {

    _categories = new ObservableCollection<PhotoCategory>();

    }

    public ObservableCollection<PhotoCategory> Categories

    {

    get

    {

    return _categories;

    }

    set

    {

    _categories = value;

    }

    }

    public void Add (PhotoCategory cat)

    {

    _categories.Add(cat);

    }

    }

     

    Compiler says :

    Error 2 'SDKSamples.ImageSample.PhotoCategories' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'SDKSamples.ImageSample.PhotoCategories.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'. C:\photoapp\csharp\PhotoCategories.cs 171 18 photoapp

    Thanks for your help !

    Regards,

    Xavier.

    Saturday, May 17, 2008 6:39 PM

Answers

  • is there a reason why you're implementing IEnumerable, instead of inheriting ObservableCollection<PhotoCategory>  ?  That would result in a lot less code for you, make things quite a bit easier.

    Sunday, May 18, 2008 10:58 AM

All replies

  • The problem is that 'SDKSamples.ImageSample.PhotoCategories' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'.

     

    Your class does contain a method  GetEnumerator but 'SDKSamples.ImageSample.PhotoCategories.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'

     

    So to be less facetious, you need to implement 'System.Collections.IEnumerable.GetEnumerator(), which returns an IEnumerator rather than an IEnumerator<T>.   This is because IEnumerable<T> (generic) is derived from the non-generic IEnumerable interface.

     

    You can do so as follows:

     

    Code Snippet

    IEnumerator IEnumerable.GetEnumerator()

    {

        return this.GetEnumerator();

    }

     

     

    Saturday, May 17, 2008 9:59 PM
  • Yes the previous respondent answered the question, a tip is also to right-click in the IDE with the mouse positioned over the "IEnumerable" that appears in the class declaration, you can then select "Implement interface explicitly" and the IDE will insert all required empty method declarations.

     

    This (if you did not already know) can reduce confusion with what is expected by C# in these kinds of settings.

     

    H

     

    Sunday, May 18, 2008 9:14 AM
  • is there a reason why you're implementing IEnumerable, instead of inheriting ObservableCollection<PhotoCategory>  ?  That would result in a lot less code for you, make things quite a bit easier.

    Sunday, May 18, 2008 10:58 AM
  • Thanks all !

    You're right. I did it without templates, works fine now.
    Forgot to use using System.Collections. Only had the Collection.Generic...


    Sunday, May 18, 2008 2:56 PM