473,698 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1373
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.Componen tModel

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.n et
Jan 5 '06 #2
"guy" <gu*@discussion s.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*@discussion s.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*@discussion s.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(COb j(...), ...)'.

--
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*@discussion s.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(COb j(...), ...)'.

--
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.Componen tModel.BindingL ist(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*@discussion s.microsoft.com > wrote in message
news:73******** *************** ***********@mic rosoft.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*@discussion s.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
1293
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 any other tool that let me?? Searching I found http://www.ericjsmith.net/codesmith/ but it works only for asp.net. Any ideas??
27
2456
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 still no can do... Any info would be great!
12
2736
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 it defines the language more completely. Great to use.
13
1689
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
1891
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 = new List<IPerson>(); ... // get the persons return personList;
18
2633
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
2478
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>> _queries; where DLinqQuery is a generic class that takes a type parameter
1
1674
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
3253
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 System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine, I understand what is being said. My question is more towards the application and implementation...
13
3823
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.