473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CollectionBase - why use it?

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
4 2175
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3509
by: Steve M | last post by:
I have subclassed CollectionBase. I have also implemented GetEnumerator(). I have tried to set the DataSource of a DataGrid to an instance of my subclass. However, the items in the grid are not...
7
1849
by: m. pollack | last post by:
Hi all, I've been using the CollectionBase class to implement a strongly-typed collection, but I have noticed that the RemoveAt method does not seem to call the "On" hook methods (OnRemove,...
2
1746
by: m.pollack | last post by:
Hi all, I have an application which uses a class object that contains a collection. In order to use the PropertyGrid control to expose properties to the user at runtime, I created a...
1
2077
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
0
2159
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
1
7067
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
5
5900
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
2
6951
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
0
1147
by: LIJO CHEERAN | last post by:
Hello friends I am trying to study about CollectionBase. I have inherited CollectionBase in the class TheCollection.cs. I am using the “TheCollection. in an aspx page to store objects...
3
2109
by: Tony Johansson | last post by:
Hello! Sorry for opening up this task again. I want to fully understand this List that is return from CollectionBase. According to you is List in CollectionBase implemented something like...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.