|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
generics questionif 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 guy wrote:
Show quoteHide quote > if i have a generic collection, say a binding list Option Explicit On> 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 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 -- To email me directly, please remove the *NO*SPAM* parts below: *NO*SPAM*xmen40@*NO*SPAM*gmx.net "guy" <g**@discussions.microsoft.com> schrieb: Where do you want to call the method? Inside the gerneic class or outside > 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) 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/> 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 Show quoteHide quote "Herfried K. Wagner [MVP]" wrote: > "guy" <g**@discussions.microsoft.com> schrieb: > > 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/> > > "guy" <g**@discussions.microsoft.com> schrieb: Try 'DirectCast(CObj(...), ...)'.> 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 -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> no luck - that gives a casting exception:(
thanks anyway Show quoteHide quote "Herfried K. Wagner [MVP]" wrote: > "guy" <g**@discussions.microsoft.com> schrieb: > > 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(...), ...)'. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> > > | i am calling the method within the generic class It sounds like you need to add a constraint to the T parameter!| there are no constraints on the parameter | > > eg dim Thing as BaseClass=CType(T,BaseClass) | > > and i know that any object in the collection will inherit from a base Is there a reason the T parameter is not constrained by BaseClass? If you | > > class 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. -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "guy" <g**@discussions.microsoft.com> wrote in message news:735B21AB-30B6-4209-A801-EC15913CA022@microsoft.com... | 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: | | > "guy" <g**@discussions.microsoft.com> schrieb: | > > 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/> | > | >
To VB or not to VB?
To VB or not to VB? Excel automation??? VS2005 - Debug and Locked Files Debugging in VS2005 - why is some code shaded grey? SqlConnection component in VB.2005 For each ..... next Question Setup and Deployment and Microsoft Jet OLE DB Provider Dir(strPath, +R +A +S +H) keeps returning hidden folders Tip of Day |
|||||||||||||||||||||||