473,403 Members | 2,222 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,403 software developers and data experts.

generics question

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
Jan 5 '06 #1
6 1355
guy 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)

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
Jan 5 '06 #2
"guy" <gu*@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/>

Jan 5 '06 #3
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

"Herfried K. Wagner [MVP]" wrote:
"guy" <gu*@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/>

Jan 6 '06 #4
"guy" <gu*@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/>

Jan 6 '06 #5
guy
no luck - that gives a casting exception:(
thanks anyway

"Herfried K. Wagner [MVP]" wrote:
"guy" <gu*@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/>

Jan 7 '06 #6
| 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
"guy" <gu*@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.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" <gu*@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/>
| >
| >
Jan 7 '06 #7

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

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
13
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.