473,394 Members | 1,735 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,394 software developers and data experts.

Typecasting of generic type

I want to cast a generic class into another. Only difference is the
type parameter used. In my original problem it would be an upcast of
the type parameter but even downcasting doesn't seem to work... I
tried the same in C# with the same results. I keep getting '....
cannot convert... ' messages in both languages.
Does anybody know if this is a limitation of the CLR and if there is a
lazy way to overcome this restriction? Or am I simply missing
something?

Thanks,
Thomas

Public Class dummy
Public Class A
End Class

Public Class B
Inherits A
End Class

Public Class C(Of T As A)
End Class

Public Function f() As C(Of A)
Return New C(Of B) ' <-- ERROR
Return CType(New C(Of B), C(Of A)) ' <-- ERROR
End Function
End Class

Oct 15 '07 #1
1 1887
<bu******@afrobacon.comwrote:
I want to cast a generic class into another. Only difference is the
type parameter used. In my original problem it would be an upcast of
the type parameter but even downcasting doesn't seem to work... I
tried the same in C# with the same results. I keep getting '....
cannot convert... ' messages in both languages.
Does anybody know if this is a limitation of the CLR and if there is a
lazy way to overcome this restriction? Or am I simply missing
something?
What you're after is called variance - and the CLR doesn't support it
in the general case. (It supports it with interfaces in some cases, but
they aren't exposed in C# anyway, and I imagine the same is true for
VB.)

See http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx for
more info.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 15 '07 #2

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

Similar topics

7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
2
by: dis | last post by:
The following code introduces a 'generic function pointer' p. When calling the pointed-to function via this p, one has to typecast p towards a pointer to the type of the pointed-to function. My...
3
by: bnoordhuis | last post by:
Consider this: int foo(int *a, int *b); int (*bar)(void *, void *) = (void *)foo; How legal - or illegal - is the typecast and are there real-world situations where such code will cause...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
11
by: John | last post by:
Hi I have a data row variable which could be for any of a number of tables in my apps. How can I type cast the variable to correct type at runtime? I know I can get the type description using...
4
by: mail.dsp | last post by:
Consider following: unsigned char i= 0; Now when we perform "++i" Is "i" typecasted into "int", "++" operator increment its value and finally typecasted into "unsigned char"? Thanks in...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.