473,397 Members | 1,985 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,397 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 1355
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: Ben | last post by:
Due to my unfamiliarity with schemas, I am unable to figure out how to accomplish the same type of processing that I have currently working under a dtd. We have a pre-defined generic message...
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...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
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...
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...
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
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,...
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
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,...

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.