Creating a custom generic class
In Golding's .Net 2.0 Generics on page 114 his class uses "(Of T As IPerson)" and on page 115 he says as soon as you attach the constraint "you have essentially eliminated much of its generic-ness." On page 144 it says you have "protected members that allow you to modify or extend the behavior." His class defines "_Collection(Of T) Inherits Collection(Of T)." Some examples have "(Of T)" for the class type as well as for the inherited type. Other examples put the generic parameter only in "Inherits Collection(Of T)." But you can't cite any properties of the type you intend to construct in the generic class because they aren''t known then. However, if you constrain the type in the definition then Intellisense should be able to locate the properties. In my class I "Override SetItem" and try to use properties of the class instance. But the compiler says it is "not accessible." That is because I can declare the constrained type in the class definition but Visual Studio won't allow it repeated again on the Inherits line. Therefore the compiler doesn't recognize it. It doesn't appear you can "have your cake and eat it too."
Public Class Test (Of T As XYZ)
Inherits Collection (Of T)
Private _testColl As Collection (Of T)
Public Sub New()
Me._testColl = New Collection (Of T)
Protected Overrides Sub SetItem (ByVal index As Integer, ByVal item As T)
MyBase.SetItem (index, item)
Dim testCase As New Test (Of XYZ)
Dim upd as New XYZ (A, B, C)
testCase.SetItem (idx, upd)
"Overload resolution failed because no 'SetItem' is accessible."
Someone just told me that a page that instantiates a specialized class can't access a protected method in that class. But Golding in .Net 2.0 Generics on page 144 says you can extend (customize) your class via Collection (T) "offering you a series of protected members that allow you to modify or extend the behavior." A module on page 146 instantiates the class and makes calls to the protected methods. The author summarizes on page 147 that "it illustrates the basic mechanics of creating your own specialization of the Collection (T) class [and] points out those protected methods that are available to you when creating a descendant collection." According to this you can access protected methods in a class you instantiate, and those overriding routines then use MyBase calls to the base class methods.
Last edited by bnorg; January 1st, 2011 at 11:54 PM.
Reason: Further research