473,395 Members | 2,437 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,395 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 7382
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.