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

Generics - How to ctype an instance of a generic class to something that I can use

I hope someone out there can help me with this, because I am stuck.

My problem is that I have an instance of a generic class, but I don't know
ahead of time what the type parameter used was. In my case the type must
derive from a class, like UserControl, so I at least know I should be able
to use that.

I can not figure out a way to get this as an instance of the generic class,
but with the base class as the type parameter. I am looking to do something
like this :

...Dim formRef as Form = /get-reference-to-mycontainer-instance/
...If ( TypeOf formRef Is MyContainer(Of UserControl) ) Then
....... Dim a As MyContainer(Of UserControl) = CType(formRef, MyContainer(Of
UserControl))
....... do stuff
...End If

Here is the generic class:
-------
...Public Class MyContainer(Of T As {UserControl, New})
.......Inherits System.Windows.Forms.Form
.......
...End Class
Is there any way to do something like this? Even something ugly?

Thanks,
//Andrew

Dec 17 '07 #1
2 2857
<Andrew Backer
</O=MARINCOUNTY/OU=CIVICCENTER/CN=RECIPIENTS/CN=ABACKER>wrote:
I hope someone out there can help me with this, because I am stuck.

My problem is that I have an instance of a generic class, but I don't know
ahead of time what the type parameter used was. In my case the type must
derive from a class, like UserControl, so I at least know I should be able
to use that.

I can not figure out a way to get this as an instance of the generic class,
but with the base class as the type parameter.
You can't - generics don't support covariance/contravariance. So, for
example, a List<stringisn't compatible with List<object>. In
particular, imagine what would happen if you tried to add a new plain
object to it...

See Eric Lippert's blog for more information on this:

http://blogs.msdn.com/ericlippert/ar...+and+Contravar
iance/default.aspx

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 17 '07 #2
So you could just use the fact that each element inherits from a user
control to do something such as :

For Each Element As UserControl In formRef
' Do something with Element
Next

without casting the list itself...

You may want to explain your overall goal. For now I'm not sure to see why
you are trying to cast this list. If the problem is to find out what is the
type of an element you could likely expose an ElementType property that
would return this...

--
Patrice
"Andrew Backer" </O=MARINCOUNTY/OU=CIVICCENTER/CN=RECIPIENTS/CN=ABACKERa
écrit dans le message de news:
67**************************@news.microsoft.com...
>I hope someone out there can help me with this, because I am stuck.

My problem is that I have an instance of a generic class, but I don't know
ahead of time what the type parameter used was. In my case the type must
derive from a class, like UserControl, so I at least know I should be able
to use that.
I can not figure out a way to get this as an instance of the generic
class, but with the base class as the type parameter. I am looking to do
something like this :

..Dim formRef as Form = /get-reference-to-mycontainer-instance/
..If ( TypeOf formRef Is MyContainer(Of UserControl) ) Then
...... Dim a As MyContainer(Of UserControl) = CType(formRef,
MyContainer(Of UserControl))
...... do stuff
..End If

Here is the generic class:
-------
..Public Class MyContainer(Of T As {UserControl, New})
......Inherits System.Windows.Forms.Form
......
..End Class
Is there any way to do something like this? Even something ugly?
Thanks,
//Andrew

Dec 19 '07 #3

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

Similar topics

7
by: Gene Vital | last post by:
Hi all, I need some help in understanding how to use Generics. I have a class based on a user control that can be put on any Container at runtime, I want to be able to call a method on the...
6
by: guy | last post by:
if i have a generic collection, say a binding list MyList as new BindingList (of T) and i know that any object in the collection will inherit from a base class that i have defined how do i call...
4
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
8
by: ThunderMusic | last post by:
Hi, We need to serialize (binary) objects containing generic collections. The thing is, when we try to get the objects back (deserialize) with a different instance of the application, we receive...
9
by: Simon Woods | last post by:
Hi I have a factory class which I want to use to create new objects which inherit a certain base type so I have a declaration Public Class MyOwnFactory(Of T As MyOwnBase) As I understand...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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,...

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.