By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
<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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.