M
Matthew Roberts
Howdy Everyone,
I am having trouble understanding the process of creating a type-safe
collection by inheriting from the CollectionBase class. I have done it
plenty of times, but now that I sit down and look at it, I'm wondering
why it behaves the way it does, and also how to improve its
functionality.
First, understand the basic format of a type-safe collection:
Public Class DerivedCollection
Inherits System.Collections.CollectionBase
Public Function Add(ByVal obj As SpecialObject) As SpecialObject
Me.List.Add(obj)
Return obj
End Function
Public Sub Remove(ByVal as SpecialObject)
Me.List.Remove(obj)
End Sub
Default Public ReadOnly Property Item(ByVal Index As Integer) As
SpecialObject
Return CType(Me.List(Index), SpecialObject)
End Property
End Class
Using the above code, how does the compiler know that the Add and
Remove methods correspond to the Add and Remove methods of the IList
interface? Meaning, since there is no Implements statement after any
of the properties or methods, how does consumer code know that my Add
and Remove are the same as standard collection functions Add and
Remove? Isn't it true that a method Implements a function to indicate
to consumer code that it handles some well known methods? Or do
collections not even support this functionality, only ILists.
I have read that the collection base is able to determine where to
find the type-safe enumerator because of the fact that the Item
property is marked Default and ReadOnly. With those keywords, the
compiler knows enough about the collection to have a type-safe
enumerator. Is that close to correct?
Now, let's take the Add and Remove methods out of the collection code
above. If we do this, then we try to use the collection in code, there
is no Add method. How then, is this a real collection? And again, by
me adding my own Add function, how is that Add function logically
related to the standard collection Add function? Is it just because
they share the same name? Or is it that collections don't actually
support Add, Remove, etc., and it is simply up to the developer to
provide that type of functionality?
If you want a custom collection to have other functionality like Sort
or Find, why wouldn't you just inherit from ArrayList?
If you want to code in IndexOf functionality or Insert functionality
like that exposed by the IList of CollectionBase, how will consumer
code know that the functions correspond to the functionality of the
IList? Meaning, if you have a function that expects an IList, then
pass your custom collection to the function, will it work? In the
function, you could access the IndexOf and Insert methods through the
interface, but would the function be able to access the custom IndexOf
and Insert methods, considering there would be no Implements statement
to relate it? Or again, does it just use the name of the method for
the association?
Any information about this subject will be greatly appreciated. I'm
sure I haven't conveyed this idea in the greatest way, so if you need
more information, I can certainly provide it.
Matthew
Please respond to:
mroberts_hm (at) hotmail (dot) com
Or
matthewroberts (at) srcp (dot) com
I am having trouble understanding the process of creating a type-safe
collection by inheriting from the CollectionBase class. I have done it
plenty of times, but now that I sit down and look at it, I'm wondering
why it behaves the way it does, and also how to improve its
functionality.
First, understand the basic format of a type-safe collection:
Public Class DerivedCollection
Inherits System.Collections.CollectionBase
Public Function Add(ByVal obj As SpecialObject) As SpecialObject
Me.List.Add(obj)
Return obj
End Function
Public Sub Remove(ByVal as SpecialObject)
Me.List.Remove(obj)
End Sub
Default Public ReadOnly Property Item(ByVal Index As Integer) As
SpecialObject
Return CType(Me.List(Index), SpecialObject)
End Property
End Class
Using the above code, how does the compiler know that the Add and
Remove methods correspond to the Add and Remove methods of the IList
interface? Meaning, since there is no Implements statement after any
of the properties or methods, how does consumer code know that my Add
and Remove are the same as standard collection functions Add and
Remove? Isn't it true that a method Implements a function to indicate
to consumer code that it handles some well known methods? Or do
collections not even support this functionality, only ILists.
I have read that the collection base is able to determine where to
find the type-safe enumerator because of the fact that the Item
property is marked Default and ReadOnly. With those keywords, the
compiler knows enough about the collection to have a type-safe
enumerator. Is that close to correct?
Now, let's take the Add and Remove methods out of the collection code
above. If we do this, then we try to use the collection in code, there
is no Add method. How then, is this a real collection? And again, by
me adding my own Add function, how is that Add function logically
related to the standard collection Add function? Is it just because
they share the same name? Or is it that collections don't actually
support Add, Remove, etc., and it is simply up to the developer to
provide that type of functionality?
If you want a custom collection to have other functionality like Sort
or Find, why wouldn't you just inherit from ArrayList?
If you want to code in IndexOf functionality or Insert functionality
like that exposed by the IList of CollectionBase, how will consumer
code know that the functions correspond to the functionality of the
IList? Meaning, if you have a function that expects an IList, then
pass your custom collection to the function, will it work? In the
function, you could access the IndexOf and Insert methods through the
interface, but would the function be able to access the custom IndexOf
and Insert methods, considering there would be no Implements statement
to relate it? Or again, does it just use the name of the method for
the association?
Any information about this subject will be greatly appreciated. I'm
sure I haven't conveyed this idea in the greatest way, so if you need
more information, I can certainly provide it.
Matthew
Please respond to:
mroberts_hm (at) hotmail (dot) com
Or
matthewroberts (at) srcp (dot) com