Wrox Programmer Forums
.NET Framework 3.5 For discussion of the Microsoft .NET Framework 3.5.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 3.5 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 November 19th, 2007, 04:53 PM
Registered User
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default NET Generics

I've got the book .Net 2.0 Generics, when I tried the following code from page 97, I receive a casting error message:

Public Function GetItems(Of T)(ByVal sql As String) As Collection(Of T)
        Dim selectCmd As New OleDbCommand(sql, dbConn)
        Dim retVal As New Collection(Of T)
        Dim dataReader As OleDbDataReader = selectCmd.ExecuteReader()
        While (dataReader.Read() = True)
            retVal.Add(DirectCast(dataReader(0), T))
        End While

        Return retVal
    End Function

I keep on getting an error:

Message="Unable to cast object of type 'System.Int32' to type 'Architecture.Employees'." What am i doing wrong?

Old February 21st, 2008, 10:17 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts


What are you passing into the T parameter? Are you passing Integer, or Employees object? How are you using this method? That will give some insight.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics in 3.0 sikanderwaheed C# 2008 aka C# 3.0 1 May 5th, 2008 08:30 AM
Application of generics micmit C# 2005 0 October 12th, 2006 08:10 PM
Generics Related error in my code, I think??? rodmcleay Generics 1 August 3rd, 2006 12:02 AM
Problem with generics collections demitry .NET Framework 2.0 4 November 24th, 2005 06:39 AM

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