473,381 Members | 1,607 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,381 software developers and data experts.

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 7380
Harold Howe <hh***@gowebway.com> wrote in news:O$7uGXeHGHA.3936
@TK2MSFTNGP12.phx.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(SolidBrush brush = new SolidBrush(BackColor)
g.FillRectangle(brush, bounds);
}
}

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

public override void Paint(Graphics g)
{
using(LinearGradientBrush 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<TBackgroundType> : 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
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...
2
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...
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
8
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
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...
3
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...
5
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...
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 { }
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.