generics question

G

Guest

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 a base class method on a member of the
collection?

i cant seem to cast from T to my base class

eg dim Thing as BaseClass=CType(T,BaseClass)

will not compile

how do i get round this?

guy
 
A

Andreas Mueller

guy said:
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 a base class method on a member of the
collection?

i cant seem to cast from T to my base class

eg dim Thing as BaseClass=CType(T,BaseClass)

will not compile

how do i get round this?

guy

Option Explicit On
Option Strict On

Imports System.ComponentModel

Class Test
Class BaseClass
Public Overridable Sub Foo()
End Sub
End Class
Class SubClass
Inherits BaseClass
Public Overrides Sub Foo()
End Sub
End Class

Shared Sub Main()
Dim myList As New BindingList(Of BaseClass)
myList.Add(New SubClass)
Dim b As BaseClass = myList(0)
b.Foo()
End Sub
End Class

HTH,
Andy
 
H

Herfried K. Wagner [MVP]

guy said:
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 a base class method on a member of the
collection?

i cant seem to cast from T to my base class

eg dim Thing as BaseClass=CType(T,BaseClass)

Where do you want to call the method? Inside the gerneic class or outside
its implementation? Did you specify a constraint on the generic parameter?
 
G

Guest

Herfried,
i am calling the method within the generic class
there are no constraints on the parameter

the cgeneric collection will be used to hold 1 of a number of classes, all
of which inherit dfrom the same base class

thanks guy
 
H

Herfried K. Wagner [MVP]

guy said:
i am calling the method within the generic class
there are no constraints on the parameter

the cgeneric collection will be used to hold 1 of a number of classes, all
of which inherit dfrom the same base class

Try 'DirectCast(CObj(...), ...)'.
 
J

Jay B. Harlow [MVP - Outlook]

| i am calling the method within the generic class
| there are no constraints on the parameter
| > > eg dim Thing as BaseClass=CType(T,BaseClass)
It sounds like you need to add a constraint to the T parameter!


| > > and i know that any object in the collection will inherit from a base
| > > class
Is there a reason the T parameter is not constrained by BaseClass? If you
know that any object in the collection will inherit from a base class, then
adding the constraint will ensure it.


Can you post 10 or 15 lines of class you are using, that shows the
relationship between the class, the T parameter, the BindingList(Of T), and
the CType? As its unclear what you really have & what you are really
trying... Can you post something like:

Public MustInherit Class BaseClass

Public MustOverride Sub DoIt()

End Class

Public Class Something(Of T As BaseClass)

Private m_list As System.ComponentModel.BindingList(Of T)

Public Sub DoIt()
For Each item As T In m_list
item.DoIt()
Next
End Sub

End Class

NOTE: In the above, because T has a BaseClass constraint, we are able to
call any BaseClass method on T variables.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Herfried,
| i am calling the method within the generic class
| there are no constraints on the parameter
|
| the cgeneric collection will be used to hold 1 of a number of classes, all
| of which inherit dfrom the same base class
|
| thanks guy
|
|
|
| "Herfried K. Wagner [MVP]" wrote:
|
| > > 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 a base class method on a member of
the
| > > collection?
| > >
| > > i cant seem to cast from T to my base class
| > >
| > > eg dim Thing as BaseClass=CType(T,BaseClass)
| >
| > Where do you want to call the method? Inside the gerneic class or
outside
| > its implementation? Did you specify a constraint on the generic
parameter?
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top