Wrox Programmer Forums
BOOK: Professional .NET 2.0 Generics
This is the forum to discuss the Wrox book Professional .NET 2.0 Generics by Tod Golding; ISBN: 9780764559884
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional .NET 2.0 Generics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 30th, 2010, 11:25 PM
Authorized User
Join Date: Aug 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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."

Imports System.Collections.ObjectModel
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)
End Sub

Protected Overrides Sub SetItem (ByVal index As Integer, ByVal item As T)
MyBase.SetItem (index, item)
End Sub

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 2nd, 2011 at 12:54 AM.. Reason: Further research
Old January 2nd, 2011, 08:50 PM
Authorized User
Join Date: Aug 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default If you don't succeed at first . . . . etc.

I decided to study the book some more. The book's test didn't call the protected methods so I just assumed it was a typographical error because why were they even there in the first place. The book didn't explain the details and neither did Visual Studio Help. On Google I found a frustrated person who asked, "Do you think it could be . . ?" I was thinking the same thing so developed a test to see if it was true, and it was. But I still couldn't get IndexOf to find an element which I knew was in the collection which makes me think that there are other mysteries to uncover. It would have to be really important to dig through all this to have to use it.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 - Creating Generic Classes mhanson BOOK: Professional C# 4.0 and .NET 4 0 November 1st, 2010 02:18 AM
Adding generic data to a class chroniclemaster1 ASP.NET 3.5 Basics 4 August 25th, 2010 12:57 PM
write a sophisticated generic class in BLL irProject BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 1 November 29th, 2009 04:05 AM
Generic List Serialisation with custom property rodmcleay C# 2005 1 April 23rd, 2007 12:52 PM
creating an event for a custom class? lkubler BOOK: Beginning Access 2003 VBA 3 September 20th, 2005 03:47 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.