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

How to use a Generic's base type?

Hi all... Heres what im looking to do.... I have a Generic class i wrote.
Now, on another class, i want to add a method which can take in an object of
my generic class...
but the catch is, i want it to be able to take in an instance of the generic
REGARDLESS of what the "Of"
type of the generic is.

E.g. .... given a generic class MyGen(Of T)

I want to be able to write another class as such:

Class MyOther
.....
Public Sub DoIt(myarg As MyGen)
... do something ...
End Sub
.....
End Class

The editor/compiler complains though that i did not specify an "Of" on the
argument myarg.
But that is specifically what i want to do, is make a method which just
takes a MyGen instance,
regardless of its "Of"

Can i do this?

Thanks in advance,
- Arthur Dent.
Jan 25 '06 #1
1 1099
Arthur,
| but the catch is, i want it to be able to take in an instance of the
generic
| REGARDLESS of what the "Of"
| type of the generic is.
Have you tried to define DoIt as a generic Method?

Something like:

| Class MyOther
| .....
| Public Sub DoIt(Of T) (myarg As MyGen(Of T))
| ... do something ...
| End Sub
| .....
| End Class

Because of "type inference" you can just call DoIt with an instance of the
generic, without specifically specifying T, and the compiler will "infer T".

Dim a As New MyGen(Of Integer)
Dim b As New MyGen(Of String)

Dim other As New MyOther

other.DoIt(a)
other.DoIt(b)

If you like you can specify the T parameter:

other.DoIt(Of Integer)(a)
other.DoIt(Of String)(b)

However there is no real benefit to specifying the type, as long as VB can
infer the type. If VB cannot infer the type based on the parameters, then
you need to give the type.
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Arthur Dent" <hi*********************@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| Hi all... Heres what im looking to do.... I have a Generic class i wrote.
| Now, on another class, i want to add a method which can take in an object
of
| my generic class...
| but the catch is, i want it to be able to take in an instance of the
generic
| REGARDLESS of what the "Of"
| type of the generic is.
|
| E.g. .... given a generic class MyGen(Of T)
|
| I want to be able to write another class as such:
|
| Class MyOther
| .....
| Public Sub DoIt(myarg As MyGen)
| ... do something ...
| End Sub
| .....
| End Class
|
| The editor/compiler complains though that i did not specify an "Of" on the
| argument myarg.
| But that is specifically what i want to do, is make a method which just
| takes a MyGen instance,
| regardless of its "Of"
|
| Can i do this?
|
| Thanks in advance,
| - Arthur Dent.
|
|
Jan 25 '06 #2

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

Similar topics

1
by: Peter Nofelt | last post by:
Hey All, I'm wondering if I'm able to have create generic user controls in .net 2.0 much like one can create generic classes. I would like to do this so that I can handle a set a types derived...
1
by: AtariPete | last post by:
Hey All, I'm wondering if I'm able to have create generic user controls in .net 2.0 much like one can create generic classes. I would like to do this so that I can handle a set a types derived...
3
by: webcliff | last post by:
Please help me to look at the following code: -------------start---------- using System; using System.Collections.Generic; public class GenType { }
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: SimonDotException | last post by:
I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
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...
0
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...

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.