473,566 Members | 3,309 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 1368
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
1288
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
2448
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
2726
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
1678
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
1882
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
2622
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
2464
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
1668
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
3243
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,...
13
3803
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...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
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...
1
7645
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...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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...
0
3643
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...
1
2085
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

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.