473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What's advantage of CollectionBase?


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
extensible base class.

For example, the documentation example implementation of Remove:

Public Class Int16Collection
Inherits CollectionBase

...

Public Sub Remove(value As Int16)
List.Remove(value)
End Sub 'Remove

...
End Class

Resolves to this call sequence:

Int16Collection.Remove ->
CollectionBase.get_List ->
((IList)CollectionBase).Remove ->
CollectionBase.InnerList.Remove

Whereas if one implemented IList directly, without collection base,
the equivalent call sequence would just be

Int16Collection.Remove ->
Int16Collection.InnerList.Remove

And this comparison is typical for all other operations as well.

So what's the advantage of using CollectionBase? Does everyone use
it? What's downside to just implementing IList?

Of course the question becomes moot when 2005 is out with Generics,
but the question is pertinent now...

Thanks,

Sam

Nov 21 '05 #1
2 6941
Samuel,
So what's the advantage of using CollectionBase? Avoiding duplication of code, as you stated: Clear, CopyTo, Count,
GetEnumerator, and RemoveAt are all publicly implemented for you. Plus you
get the rest of the IList members privately implemented for you (about 11
more functions).

All you need to add to your derived class is the specific type safe methods.
Alternatively you can override the On* methods to ensure that the IList
interface itself remains type safe.
Public Sub Remove(value As Int16)
List.Remove(value)
End Sub 'Remove You do know the difference & importance of using CollectionBase.List &
CollectionBase.InnerList are?

Normally I use CollectionBase.InnerList as it represents the contained
ArrayList itself. Using CollectionBase.List ensures the On* methods to be
called, I use CollectionBase.List when I want the public method to also call
the On* method, for example when my derived CollectionBase class
encapsulates a HashTable also. For example:

Public Class PersonCollection
Inherits CollectionBase

Private Readonly m_table As New HashTable

Public Sub Remove(value As Person)
List.Remove(value)
End Sub 'Remove

Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, _
ByVal value As Object)
Dim person As Person = DirectCast(value, Person)
m_table.Remove(person.Name)
End Sub

...

End Sub

When a Person is removed from the collection, via either Remove or
IList.Remove it will be removed from the contained HashTable also.

NOTE: PersonCollection is an ordered list before its a Dictionary, hence it
inherits from CollectionBase instead of DictionaryBase. If it inherited from
DictionaryBase it would contain an ArrayList to maintain the order...
Alternatively PersonCollection could inherit from
NameObjectCollectionBase...
Does everyone useit? I normally use CollectionBase, or I have my own variation of it.
What's downside to just implementing IList? If you are defining a number of lists, the downside is you need to duplicate
the implementation of IList in a number of lists.

Duplication of code is "bad" in a number of OO circles.
Of course the question becomes moot when 2005 is out with Generics,
but the question is pertinent now... Last I read Generics are not CLS compliant, using CollectionBase for public
CLS compliant classes remains pertinent in 2005!

Hope this helps
Jay

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:9e********************************@4ax.com...
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
extensible base class.

For example, the documentation example implementation of Remove:

Public Class Int16Collection
Inherits CollectionBase

...

Public Sub Remove(value As Int16)
List.Remove(value)
End Sub 'Remove

...
End Class

Resolves to this call sequence:

Int16Collection.Remove ->
CollectionBase.get_List ->
((IList)CollectionBase).Remove ->
CollectionBase.InnerList.Remove

Whereas if one implemented IList directly, without collection base,
the equivalent call sequence would just be

Int16Collection.Remove ->
Int16Collection.InnerList.Remove

And this comparison is typical for all other operations as well.

So what's the advantage of using CollectionBase? Does everyone use
it? What's downside to just implementing IList?

Of course the question becomes moot when 2005 is out with Generics,
but the question is pertinent now...

Thanks,

Sam

Nov 21 '05 #2

The main advantage of inheriting CollectionBase is the creation of strongly
typed collections. Do you this your class needs to inherit ColloectionBase
and then override its methods and properties that take or return Object to
replace it with your object.

However, this is quite time consuming and requires you to write a new class
for every strongly typed collection. VB.Net 2005 solves this by further
moving VB into the Object Oriented Programming by adding Generic, or as they
are called in C++, Template classes.

Robby

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:9e********************************@4ax.com...

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
extensible base class.

For example, the documentation example implementation of Remove:

Public Class Int16Collection
Inherits CollectionBase

...

Public Sub Remove(value As Int16)
List.Remove(value)
End Sub 'Remove

...
End Class

Resolves to this call sequence:

Int16Collection.Remove ->
CollectionBase.get_List ->
((IList)CollectionBase).Remove ->
CollectionBase.InnerList.Remove

Whereas if one implemented IList directly, without collection base,
the equivalent call sequence would just be

Int16Collection.Remove ->
Int16Collection.InnerList.Remove

And this comparison is typical for all other operations as well.

So what's the advantage of using CollectionBase? Does everyone use
it? What's downside to just implementing IList?

Of course the question becomes moot when 2005 is out with Generics,
but the question is pertinent now...

Thanks,

Sam

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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
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
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
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
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
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
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...
1
by: Morten Dyndgaard | last post by:
Hi, I want to do the following: I have to XmlSerialize a HashTable, but since it inherits from IDictionary it cannot be XmlSerialized, and I prefer not to make my own implementation of...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.