473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

value of type 'T' cannot be converted to...

Hi,

I'm getting in trouble with the Generic.List(Of T).

When declaring a Shadoiws of Mybase.Add, I want to do some custom actions
with the item, and cast it to another class. The problem is that I get the
whole time this error: "value of type 'T' cannot be converted to
'clsBaseClass'".

I get it with both CType and Directcast:

Public Class TestList(Of T)
Inherits List(Of T)

Public Shadows Sub Add(ByVal item As T)
Dim cls As New clsBaseClass
cls = CType(item, clsBaseClass) '-> gives the error
cls = DirectCast(item, clsBaseClass) '-> gives the error
End Sub

End Class
Any help our hints would be really appreciated!

Thanks a lot in advance,

Pieter
Nov 21 '05 #1
2 8713
DraguVaso wrote:
I'm getting in trouble with the Generic.List(Of T).

When declaring a Shadoiws of Mybase.Add, I want to do some custom actions
with the item, and cast it to another class. The problem is that I get the
whole time this error: "value of type 'T' cannot be converted to
'clsBaseClass'".

I get it with both CType and Directcast:

Public Class TestList(Of T)
Inherits List(Of T)

Public Shadows Sub Add(ByVal item As T)
Dim cls As New clsBaseClass
cls = CType(item, clsBaseClass) '-> gives the error
cls = DirectCast(item, clsBaseClass) '-> gives the error
End Sub

End Class


You see this problem because the compiler doesn't know anything about the
type T at this point, so it can't allow you to convert it into any other
type. If you are sure that the types that need to be handled will always
derive from clsBaseClass, you can use a constraint to tell the compiler so
and the cast will work. I don't normally use VB.NET, but I think the
syntax to do so should be this:

Public Class TestList(Of T As clsBaseClass)
Inherist List(Of T)
...

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 21 '05 #2
Thanks a lot!!! You're a hero!! I finally have what I wanted with this small
piece of code! :-) Great! you made my day! hehe :-)

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
DraguVaso wrote:
I'm getting in trouble with the Generic.List(Of T).

When declaring a Shadoiws of Mybase.Add, I want to do some custom actions
with the item, and cast it to another class. The problem is that I get the
whole time this error: "value of type 'T' cannot be converted to
'clsBaseClass'".

I get it with both CType and Directcast:

Public Class TestList(Of T)
Inherits List(Of T)

Public Shadows Sub Add(ByVal item As T)
Dim cls As New clsBaseClass
cls = CType(item, clsBaseClass) '-> gives the error
cls = DirectCast(item, clsBaseClass) '-> gives the error
End Sub

End Class


You see this problem because the compiler doesn't know anything about the
type T at this point, so it can't allow you to convert it into any other
type. If you are sure that the types that need to be handled will always
derive from clsBaseClass, you can use a constraint to tell the compiler so
and the cast will work. I don't normally use VB.NET, but I think the
syntax to do so should be this:

Public Class TestList(Of T As clsBaseClass)
Inherist List(Of T)
...

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chumley the Walrus | last post by:
I'm using this sql parameter: MyCommand.Parameters("@Sport").value = Server.HtmlEncode(sport) to get a value from a textbox control: <asp:TextBox Name="sport" id="sport"...
9
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
12
by: Francois Grieu | last post by:
The values of ((int)0.7) and ((int)-0.7) seem to be 0 Is this independent of implementation ? TIA, François Grieu
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
2
by: Joe | last post by:
Hello, I'm trying to deserialize my objects (which derive from CollectionBase) using the special constructor. Everything works fine until I go to set the value. I get the following error:...
2
by: Sean | last post by:
Hello, I have a function in C# that accepts a stored proc name and an array of parameters, and then returns a dataset, which works just fine. I have converted the function to VB: Public Shared...
6
by: Sameh Ahmed | last post by:
Hello there I need to get the "PwdLastSet" of a user object to know when he last set his password. I am using DirectoryServices.DirectoryEntry to bind to the user object, but it either gives...
6
by: baret bonden | last post by:
I get :Value of type 'String' cannot be converted to '1-dimensional array of String' refering to curitem Dim curItem As String curItem = ListBox1.SelectedItem TextBox1.Text = curItem ...
2
by: Jim in Arizona | last post by:
I'm learning form an ASP.NET 1.0 book and I tried out some code that returns this error: Compiler Error Message: BC30311: Value of type 'Integer' cannot be converted to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.