Home All Groups Group Topic Archive Search About
Author
5 Jan 2006 2:32 PM
guy
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

Author
5 Jan 2006 3:47 PM
Andreas Mueller
guy wrote:

Show quoteHide quote
> 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
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Author
5 Jan 2006 4:54 PM
Herfried K. Wagner [MVP]
"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/>
Author
6 Jan 2006 8:01 AM
guy
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/>
>
>
Author
6 Jan 2006 12:36 PM
Herfried K. Wagner [MVP]
"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/>
Author
7 Jan 2006 2:31 PM
guy
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/>
>
>
Author
7 Jan 2006 5:55 PM
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


Show quoteHide quote
"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/>
| >
| >