Custom Collection Confusion

C

craig

Assume that you would like to create a custom collection class that is:

1. Strongly-typed (only holds Customer objects)
2. Read-only (user cannot add Customer objects)
3. Able to be bound to a winforms datagrid (must implement the IList
interface).

What would be a good way to create this class?

1. The CollectionBase abstract base class can be used, but it is not
read-only.
2. The ReadOnlyCollection abstract base class can be used, but it doesn't
implement IList and therefore can't be bound.
3. The IList interface includes an "IsReadOnly" property, so why does the
ReadOnlyCollection base class
not implement the IList interface, with IsReadOnly set to return "true"??
(Instead, it doesn't implement IList at all).
 
M

MHelland

What would be a good way to create this class?

This seems be a very common question and I'm often confused by the varying
answers posted here.

I'm wondering if those with an opinion would be willing to post to a
WikisWeb for future reference?
Go here: http://dotnet.wikis.com/wc.dll?dotnet~CollectionClasses
And click "Edit" to modify the contents of the page to add your two cents.

Just an idea.
 
N

Nicholas Paldino [.NET/C# MVP]

Craig,

I would ignore the last post. It's just spam for a C# site. The text
is so generic I could post it in a gardening newsgroup and get the same
thing. As an example, check out what I posted:

http://dotnet.wikis.com/wc.dll?DotNet~IThinkYourSpamSucks

What I would do is create a class that extends ReadOnlyCollectionBase,
and implement IList yourself, throwing InvalidOperationExceptions on the
implementation of Add, Remove, etc, etc that would violate the read-only
status.

Then I would create the custom collection as normal, but not include
type-safe methods for Remove, Add, and the set indexer.

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

I just received an email from Steven Black about this, and apparently, I
might have erred. However, I do have to say that your post had a few items
in it that led me to respond in that manner:

You wrote:
---
What would be a good way to create this class?

This seems be a very common question and I'm often confused by the varying
answers posted here.
---

Your post was initially the only response to the original poster's post
in this group. I took "here" to mean this group. Based on that, it would
seem that the responder did not really read the post, or it was automatic.
On top of that, a search on google.com for this group only turns up 16
results for ReadOnlyCollectionBase dating back to July of 2001.

In retrospect, I feel that I did act in haste, and I apologize for that.
As I stated in my email to Steven Black, I have no problem with people
promoting their sites that promote .NET and knowledge in general, but in my
initial analysis of the response, I felt that the promotion was coming under
the guise of helping the original poster, which it certainly did not.

My apologies to anyone that might have been offended. The reply was in
no way meant to criticize the content or the efficacy of the site, but
rather, the methods used to promote it. Looking back, I feel that I have
erred, and I apologize for that.
 
R

Randy Jean

What are you, a newsgroup Nazi? - I just came out here as a result of
reading your post. Now I am reminded why I hate newsgroups. I get a
headache trying to find anything of use out here. Wiki's are a much
better place to share knowledge. Mike's right, you don't know what
you're talking about.
 
R

Randy Jean

In light of this apology, I hope you'll accept my apology to you and I
would also like to retract my calling you a newsgroup Nazi. That was
uncalled for.
 
M

Mike Helland

On top of that, a search on google.com for this group only turns up 16
results for ReadOnlyCollectionBase dating back to July of 2001.

Yes, well, I too probably posted my message to the wrong thread. A couple
days ago there was a thread called:

'Bad design of C# collections framework. Where is the "Set" collection?'

And similar ones have seemed to appear every couple of days. I was going to
post the wikis recommendation to Bad Design one, but decided against it.
When I saw the new collection thread today, I figured I might as well chuck
it out there, not seeing that the question was specifically about read only
collections.

I'd still like to see a central wikis topic on clr collections, if you have
anything to add, that'd be cool.
 
N

Nicholas Paldino [.NET/C# MVP]

Randy,

Thank you, it is much appreciated, and apology accepted.

However, I don't know if I can forgive you for the following things:

- Criticism of the technology used on my personal site.
- Criticism of where I was brought up.

Just kidding =)
 
R

Rakka Rage

http://www.sellsbrothers.com/tools/

"CollectionGen is a Custom Tool Add-In to VS.NET 2002 & 2003 to
generate type-safe collections. As it turns out, I did almost none of
the work. Jon Flanders figured out how to add a custom tool. Shawn Van
Ness implemented the template for type-safe collections. I just put it
together.

CollectionGen is an add-on to generate code for type-safe collections
until we have templates in C# (likely) and VB (unlikely). The benefit
of a type-safe collection, of course, is that you can use it without
having to cast items to and from objects. Also, Shawn has been very
careful to implement a collection class that is very efficient for
both reference types and value types.

Once you've setup it up and defined your collections in a collection
definition file in your project, you'll have type-safe collection
classes generated as part of your design-process, as shown here:"
 
B

bjs10

Since CollectionBase implements IList, derive a class from
CollectionBase and implement IsReadOnly in that class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top