473,568 Members | 2,939 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.Collecti ons.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.It em(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 2180
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******** *************@f 14g2000cwb.goog legroups.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.Collecti ons.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.It em(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******** *************@f 14g2000cwb.goog legroups.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.Collecti ons.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.It em(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.Collecti ons.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.It em(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.Collecti ons.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.It em(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
3517
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 obtained via the Enumerator that I create in the GetEnumerator() method. I have tried elplicitly implementing IEnumerable on my sublass as well. Can...
7
1854
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, OnRemoveComplete). The Add method works fine, though! I need these to work -- I am exposing the collection to the user through a PropertyGrid control,...
2
1755
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 strongly-typed collection class based on CollectionBase. However, when I use the PropertyGrid to remove objects from the collection at runtime via the popup...
1
2086
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(). However, CollectionBase implements IList which also defines method RemoveAt(). In my collection when I code my own RemoveAt() method, the compiler...
0
2162
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 and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only...
1
7078
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 and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only...
5
5906
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 Count { get { return 0; }
2
6953
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, GetEnumerator, and RemoveAt) but the way it implements all the other things you need to override seems overkill and counters the advantage of having an...
0
1150
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 of the class ElementClass.cs. When I click the Button2 an exception occurs….index out of range….This happens because of the fact that the class I...
3
2112
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 the below which you sent me previously. So the referenced type for List that will be returned at execution time must be CollectionBase. The compile...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
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 we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.