Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Generic ReadOnlyCollection sealed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Boisvert - 05 Aug 2005 00:22 GMT
I'm trying to create a custom read only collection. I found the generic
ReadOnlyCollection class and the documentation says "Implementers are
encouraged to extend this base class instead of creating their own."
OK, sounds good. The only problem is that it seems that most of the
methods of ReadOnlyCollection<T> are sealed, which makes extending the
class difficult. Have I missed something? Thanks.

Ian.
Oliver Sturm - 05 Aug 2005 10:12 GMT
> I'm trying to create a custom read only collection. I found the generic
> ReadOnlyCollection class and the documentation says "Implementers are
> encouraged to extend this base class instead of creating their own."
> OK, sounds good. The only problem is that it seems that most of the
> methods of ReadOnlyCollection<T> are sealed, which makes extending the
> class difficult. Have I missed something? Thanks.

I guess you mean the methods are not virtual, right? In that case yes,
you are right. But there's nothing wrong with that, it's the same
pattern that's been used for all the other generic collection classes.
This shouldn't normally be much of a problem because the generic
collection class is typed already. You can derive your own type from it
and extend it with additional methods, if you want, or you can use it as
it is, because in contrast to the non-generic [ReadOnly]CollectionBase,
this is not an abstract class.

What exactly do you want to do in your derived class? Why is it a problem?

               Oliver Sturm
Signature

omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Ian Boisvert - 05 Aug 2005 20:33 GMT
Thanks, Oliver. What I want to be able to do is to provide a custom
enumerator so that I can iterate through only specific objects in a
"master collection" which the read-only collection wraps. To do this I
need to overload IEnumerable<T>::GetEnumerator (and
ICollection<T>::Count).

Ian.
Oliver Sturm - 07 Aug 2005 14:26 GMT
> Thanks, Oliver. What I want to be able to do is to provide a custom
> enumerator so that I can iterate through only specific objects in a
> "master collection" which the read-only collection wraps. To do this I
> need to overload IEnumerable<T>::GetEnumerator (and
> ICollection<T>::Count).

Why don't you just create an additional iterator instead of overriding
the standard one?

  class MyCollection : ReadOnlyCollection<MyContent> {
    ...
    public IEnumerable<MyContent> MyNewIterator() {
      foreach (MyObject mo in this)
        if (mo.SatisfiesMyCriteria())
          yield return mo;
    }
    ...
  }

Actually, if I understand the scenario correctly that you are
describing, you could just as well implement this additional iterator
(or any number of them) directly in your "master collection". You can
even pass in parameters to the iterator to make it more flexible...

               Oliver Sturm
Signature

omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Ian Boisvert - 08 Aug 2005 09:00 GMT
Oliver, thanks for the suggestion. If I created a new method to return
a different enumerator, I would have a schitzophrenic collection which
is not a good solution IMHO.

For the record, I rolled my own read only collection class. I had some
trouble until I realized that you have to implement both of
IEnumerable::GetEnumerator and IEnumerable<>::GetEnumerator using
something like:
virtual IEnumerator^ GetEnumerator() = IEnumerable::GetEnumerator()
{...}
virtual IEnumerator<T>^ GetGenericEnumerator() =
IEnumerable<T>::GetEnumerator() {...}

So much for not having to reinvent the wheel...

Ian.
Oliver Sturm - 08 Aug 2005 10:02 GMT
> Oliver, thanks for the suggestion. If I created a new method to return
> a different enumerator, I would have a schitzophrenic collection which
> is not a good solution IMHO.

Well, that's the solution devised by the .NET team, and I like it. An
iterator is not about what a collection contains, but about enumerating
elements in a collection in a specific way. So I don't think you have a
schizophrenic collection, you just have a collection that knows how to
distinguish between different types of its elements and enumerate each
type accordingly.

> For the record, I rolled my own read only collection class. I had some
> trouble until I realized that you have to implement both of
[quoted text clipped - 6 lines]
>
> So much for not having to reinvent the wheel...

Under many circumstances it's enough to have one of those, if I'm not
mistaken. It's also possible to implement the generic one "for real",
meaning with the algorithm that's needed to select the correct data you
need, and to just implement the non-generic method like this:

  IEnumerator IEnumerable.GetEnumerator() {
    foreach (T item in this)
      yield return item;
  }

               Oliver Sturm
Signature

omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.