473,738 Members | 10,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1384
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******** ********@tk2msf tngp13.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
3866
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 header that must appear on all XML messages in our shop. We have several processes that just need to process the generic header to determine how to route the message and which service needs to process the message. So we have an XML structure that at...
1
4108
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 from the same base type within a user control Consider the following scenario:
25
3031
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 aware of that fact that if you have a class B, that inherits from A, then List<Bdoes NOT inherit from List<A>. So I understand why the example below does not compile, but I fail to
2
9864
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
3241
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 subclass of Logical (LogicalA, LogicalB). I'm currently casting them to the right type when I retrieve them, but obviously this isn't terribly 'nice'. I tried converting the class to be generic, and having a type parameter that affected the type of...
9
5849
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
10
1936
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 I will need the Generic Class, given there are so many other options. -- cheers,
3
2286
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
4183
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
2987
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 completely satisfies FxCop code analysis. Here is the simplest form of the base class which reproduces the problem: public abstract class XmlSerializableItem<T> { public string ToXml() { XmlSerializer xs = new XmlSerializer( this.GetType() );...
0
8969
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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
6053
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.