CollectionBase - why use it?

D

dotnw

If the answer is "to make strongly typed collections", then I can do
this with an ArrayList by taking advantage of the Shadows keyword, as
follows:

Public Class clsMyClass
Inherits System.Collections.ArrayList

Public Shadows Sub Add(ByVal o As clsMyItem)
MyBase.Add(o) '.Add only allows objects of type clsMyItem to be
added to this ArrayList class.
End Sub

Public Shadows Property Item(ByVal i As Integer) As clsMyItem
Get
Return CType(MyBase.Item(i), clsMyItem)
End Get
Set(ByVal Value As clsMyItem)
MyBase.Item(i) = Value
End Set
End Property
End Class

There must be other reasons for using a CollectionBase, but what are
they please?

Regards, dnw.
 
C

Carlos J. Quintero [.NET MVP]

I think that if somebody casts your clsMyClass to an ArrayList, he will be
able to add objects of other types to it (not clsMyItem). The CollectionBase
allows you to override OnInsert, OnValidate, etc. and to prevent that.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
B

Bob Powell [MVP]

You've had to do the same amount of work to coerce the ArrayList in a pretty
lousy OOP example when the CollectionBase is a great blank-slate upon which
to draw.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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