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

CollectionBase - why use it?

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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


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

<do***@hotmail.com> escribió en el mensaje
news:11*********************@f14g2000cwb.googlegro ups.com...
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.

Nov 21 '05 #2

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

<do***@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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.

Nov 21 '05 #3

P: n/a
dnw,

Here is the best article I have seen on type-safe collections in .Net:

http://www.ondotnet.com/pub/a/dotnet...ns.html?page=1

Kerry Moorman

"do***@hotmail.com" wrote:
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.

Nov 21 '05 #4

P: n/a
Thanks for all replies, especially Kerry's excellent link.

Kerry Moorman wrote:
dnw,

Here is the best article I have seen on type-safe collections in .Net:

http://www.ondotnet.com/pub/a/dotnet...ns.html?page=1

Kerry Moorman

"do***@hotmail.com" wrote:
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.


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.