473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic argument as a base class

Howdy all.

Is it possible to use a generic argument as a base class? Based on the
error that I am getting, I think the answer is no.

public class Test<T> : T
{

}

main.cs(5,24): error CS0689: Cannot derive from 'T' because it is a type
parameter
This feature would be most useful, particularly in the design of a class
library. It would allow users to inject different behaviors into the
leaf classes in the hierarchy.

H^2
Jan 20 '06 #1
3 7395
Harold Howe <hh***@gowebway .com> wrote in news:O$7uGXeHGH A.3936
@TK2MSFTNGP12.p hx.gbl:
This feature would be most useful, particularly in the design of a class
library. It would allow users to inject different behaviors into the
leaf classes in the hierarchy.


I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything... What/how would you actually use the Test<T>
class? It would be exactly the same as the T class itself. You wouldn't
be able to override any methods of T because the programmer (not to mention
the compiler) doesn't know what the methods of T are.

Maybe you are searching for 'interfaces'?

-mdb
Jan 20 '06 #2
I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything...


This is a common technique for implementing the strategy pattern,
especially in other languages. See Modern C++ by Alexandrescu.

Here is a contrived example that uses graphical visio like objects.

class BaseNode
{
public Rectangle Bounds;
public Point Location;
public Size Size;
public virtual void Paint(Graphics g){}
}

class SolidNode
{
public Color BackColor;
public override void Paint(Graphics g)
{
using(SolidBrus h brush = new SolidBrush(Back Color)
g.FillRectangle (brush, bounds);
}
}

class GradientNode
{
public Color StartColor;
public Color EndColor;
public float Angle;

public override void Paint(Graphics g)
{
using(LinearGra dientBrush brush ... )
g.FillRectangle (brush, bounds);
}
}

class EllipticalNode< TBackgroundType > : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// set eliptical clip region
// paint background
base.Paint(g);

// paint elliptical border
g.DrawElipse(.. .)
}
}
class BoxNode<TBackgr oundType> : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// paint background
base.Paint(g);

// paint rectangular border
g.DrawRectangle (...)
}
}

The purpose of the templates is to alter the behavior of the leaf
classes via base class selection. Here, the base class dictates how the
background of the node is painted. Without generics, the two leaf
classes would balloon to four classes (GradientBox, SolidBox,
GradientElipse, SolidElipse), or the classes would need to use
aggregation (ie you would pass them an object that is responsible for
painting the background).

H^2
Jan 20 '06 #3
Harold,
Is it possible to use a generic argument as a base class? Based on the
error that I am getting, I think the answer is no.

public class Test<T> : T
{

}

main.cs(5,24): error CS0689: Cannot derive from 'T' because it is a type
parameter


The reason this is not allowed is that the type of T is not known at
compile-time. Therefore, the compiler does not know what the superclass of
Test<T> is.

I know it seems that if the compiler can figure out <T>, the compiler should
be able to figure out : T. However, these are two are different things in
reality. <T> represents the generic type that the compiler expects
developers to define when creating instances of the Test<T> class, when
extending the class, etc. However, : T defines the superclass of Test<T>.
Since T is unknown at compile time, Test<T> can not inherit from T.

I hope that helps.

Regards,

Randy
Jan 21 '06 #4

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

Similar topics

1
1366
by: Arthur Dent | last post by:
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 ...
2
1520
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of the conversion. I have trimmed my source down as much as possible. The classes implement something that behaves sort of like the Mediator design...
2
9860
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
8
3907
by: kasper.rung | last post by:
I have a problem formulating a test to see if an object is implementing a generic interface. I have the following: public interface IGeneric<T> { void Foo(); }
9
12809
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The...
3
4262
by: =?Utf-8?B?Sm9hcXVpbg==?= | last post by:
OK, this was hard to explain on a short title. Sorry. I have an abstract class (MustInherit) named Base, and a generic collection class named ColBase which can only take Base and derived classes as it's type. I also have an indefinite number of classes that inherit Base. I want to make a method on Base that returns a ColBase(of Base), but...
5
26070
by: Anders Borum | last post by:
Hello! I'm trying to call a generic method by providing the T parameter at runtime, as opposed to compile time (i.e. by specifying the T parameter in the method call). I was under the impression that the CLR was able to infer the T type dynamically - perhaps I was wrong or simply "asking the wrong question"? :-) public void...
3
2278
by: webcliff | last post by:
Please help me to look at the following code: -------------start---------- using System; using System.Collections.Generic; public class GenType { }
0
2961
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...
0
7694
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
7609
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
7921
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
7964
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...
0
6278
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...
0
3651
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
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...

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.