By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

Custom Collection Confusion

P: n/a
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).
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
> 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.
Nov 13 '05 #2

P: n/a
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?DotNe...kYourSpamSucks

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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"craig" <e@mail.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
What is a WikisWeb??

"MHelland" <mh******@techmocracy.net> wrote in message
news:Op**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 13 '05 #3

P: n/a
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.

Nicholas Paldino [.NET/C# MVP] wrote:
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?DotNe...kYourSpamSucks

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.


Nov 13 '05 #4

P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Mike Helland" <mh******@techmocracy.net> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
It's just spam for a C# site.


You don't know what you're talking about.

Nov 13 '05 #5

P: n/a
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.

Nicholas Paldino [.NET/C# MVP] wrote:
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.


Nov 13 '05 #6

P: n/a
> 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.
Nov 13 '05 #7

P: n/a
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 =)

--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Randy Jean" <no**********@ogse.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Nov 13 '05 #8

P: n/a
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:"
"craig" <e@mail.com> wrote in message news:<u8**************@TK2MSFTNGP11.phx.gbl>...
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).

Nov 13 '05 #9

P: n/a
Since CollectionBase implements IList, derive a class from
CollectionBase and implement IsReadOnly in that class.

"craig" <e@mail.com> wrote in message news:<u8**************@TK2MSFTNGP11.phx.gbl>...
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).

Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.