Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > BOOK: Professional .NET 2.0 Generics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 30th, 2010, 10:25 PM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Spokane, WA, USA.
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 1st, 2011 at 11:54 PM. Reason: Further research
Reply With Quote
  #2 (permalink)  
Old January 2nd, 2011, 07:50 PM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Spokane, WA, USA.
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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 03: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



All times are GMT -4. The time now is 12:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.