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

Strongly Typed Collections and the Collection Object

P: n/a
Hi,

I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...

1) Creating a strongly typed collection by inheriting CollectionBase.

This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a parameter and then calls List.Add from the base class. OK.

However CollectionBase exposes the IList interface as an explict interface. I want to be able to override the Add method in the IList interface. Why? Because if someone casts my collection to an IList, they can bypass my strongly typed implementation. (My implementation of IList.Add would check the type of the passed object and throw an exception if it was wrong).

If I try saying "Implements IList" in my subclass it's rejected since CollectionBase implements it. If I try to override or set my own method to Implement IList.Add then it's rejected because my subclass doesn't implement IList!!!

What have I missed? How can I override the implementation of the IList interface in my subclass

2) Creating a strongly typed collection using the Collection Object.

CollectionBase does not support the "key" type lookup for collections (like VB6 collections). In order to have a key-based collection I need to use the Collection object (I think).

I want to have a strongly typed collection by overriding the Collection object methods with my own typed methods. However, I can't inherit the Collection object since it's marked as NotInheritable.

So I have to implement my own class that "front-ends" the Collection object in the same way as was done in VB6.

Again, have I missed something? Is there a key-based collection class that I can inherit?

Thanks.
Ian Gore
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
As For point 1.
You could implement empty stubs in the SubClass, and then from each sub call
the base function passing the parameters on, this should work tho I have not
tried it.

As For Point2.
You should check out the Specialized Collection namespace, these may be
inheritable and you could make use of them.

HTH - OHM

"Ian Gore" <Ia*****@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
Hi,

I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...
1) Creating a strongly typed collection by inheriting CollectionBase.

This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a
parameter and then calls List.Add from the base class. OK.
However CollectionBase exposes the IList interface as an explict interface. I want to be able to override the Add method in the IList
interface. Why? Because if someone casts my collection to an IList, they
can bypass my strongly typed implementation. (My implementation of IList.Add
would check the type of the passed object and throw an exception if it was
wrong).
If I try saying "Implements IList" in my subclass it's rejected since CollectionBase implements it. If I try to override or set my own method to
Implement IList.Add then it's rejected because my subclass doesn't implement
IList!!!
What have I missed? How can I override the implementation of the IList interface in my subclass
2) Creating a strongly typed collection using the Collection Object.

CollectionBase does not support the "key" type lookup for collections (like VB6 collections). In order to have a key-based collection I need to
use the Collection object (I think).
I want to have a strongly typed collection by overriding the Collection object methods with my own typed methods. However, I can't inherit the
Collection object since it's marked as NotInheritable.
So I have to implement my own class that "front-ends" the Collection object in the same way as was done in VB6.
Again, have I missed something? Is there a key-based collection class that I can inherit?
Thanks.
Ian Gore

Nov 20 '05 #2

P: n/a
1. Override OnInsertComplete and throw an exception from
there if the type is wrong. This method is called from inside
the internal implementation of IList.Add and IList.Insert
2. Try NameObjectCollectionBase

/claes

"Ian Gore" <Ia*****@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
Hi,

I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...
1) Creating a strongly typed collection by inheriting CollectionBase.

This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a
parameter and then calls List.Add from the base class. OK.
However CollectionBase exposes the IList interface as an explict interface. I want to be able to override the Add method in the IList
interface. Why? Because if someone casts my collection to an IList, they
can bypass my strongly typed implementation. (My implementation of IList.Add
would check the type of the passed object and throw an exception if it was
wrong).
If I try saying "Implements IList" in my subclass it's rejected since CollectionBase implements it. If I try to override or set my own method to
Implement IList.Add then it's rejected because my subclass doesn't implement
IList!!!
What have I missed? How can I override the implementation of the IList interface in my subclass
2) Creating a strongly typed collection using the Collection Object.

CollectionBase does not support the "key" type lookup for collections (like VB6 collections). In order to have a key-based collection I need to
use the Collection object (I think).
I want to have a strongly typed collection by overriding the Collection object methods with my own typed methods. However, I can't inherit the
Collection object since it's marked as NotInheritable.
So I have to implement my own class that "front-ends" the Collection object in the same way as was done in VB6.
Again, have I missed something? Is there a key-based collection class that I can inherit?
Thanks.
Ian Gore

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.