Strongly Typed Collections and the Collection Object

G

Guest

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
 
O

One Handed Man \( OHM#\)

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 said:
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.
 
C

Claes Bergefall

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 said:
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.
 

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