473,847 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Down-casting of Typed Collection (Casting Generic Types?)

Is there an elegant way to have a class utilise an inherited method to
return a Typed Collection of itself?


class MyList<T> : List<T> where T : MyRoot

abstract class MyRoot
public static MyList<MyRoot> List(...)
// return MyList<MyRoot>
MyList list = new MyList<MyRoot>( );
MyRoot obj = new MyRoot();
return list;

class MyObj : MyRoot
public static MyList<MyObj> List(...)
// need to cast MyList<MyRoot> as MyList<MyObj>
return (MyList<MyObj>) base.List(...);

Obviously this simplified code is seriously flawed - but hopefully you
understand what I'm trying to achieve.

I want a static method of MyObj to return a List of MyObj objects, but
the implementation of this method should ideally be in the abstract
base class. The actual scenario I'm playing with is a Data Access
Layer using a Collection of data objects with a shared base which
handles all the database connectivity. The objects simply expose the
resultant fields as properties and inherit the Save(), Load(), List()

Is it possible to have an arbitrary number of static methods (some
static) which return a Collection of the expected type by calling the
relevant base class method? I'm trying to encapsulate as much of the
code as possible in th base class to make the derived classes as simple
as possible.

// static method on derived class
ObjectList<Pers on> ukPeople = Person.ListByCo untry("UK");

// instance method on derived class
Person bob = new Person("bob");
ObjectList<Pers on> bobsPeople = bob.ListStaff() ;
Any ideas or pointers?


Jun 29 '06 #1
1 7963
This has been neatly answered in another thread a day later ("Question
about Generics" by hammad_awan mirrored at
so I thought I should close this one by pointing interested parties
over there instead.

In summary, this sort of casting is known as 'covariance' and is not
supported by the C# language at present (although it is apparently
supported at compiler level).

Barry Kelly posted a link to an interesting article on a proposed C#
extension to add this functionality:


Jul 3 '06 #2

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

Similar topics

by: mead | last post by:
The code is from a Meyers' book... class BankAccount { ... }; // as above // new class representing accounts that bear interest class InterestBearingAccount: public BankAccount { public: virtual void creditInterest() = 0; ...
by: Ken Allen | last post by:
I am contemplating a rather complex inplementation that calls for a factory class that can instantiate a large number of specific business objects, each of which share a set of common base business rules (e.g., Update, Delete, etc.) as well as a set of object specific methods. Of couse, all of the interfaces shall be defined in a common assembly for the client and server components. Since all of these are remoted, presume that the...
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran into a problem using C-style casting to an instance of a derived class from its interface type to a variable of its concrete type. I was confused because I thought C-style casts always compiled even if they wouldn't run correctly at runtime. I...
by: Tem | last post by:
I have classes A,B,C,D,E,F that implement InterfaceBase. Whats the best way to down cast obj to the correct type? Is there a more elegant way to do this than? Public Void DoSomething(InterfaceBase obj) { if(obj is A) ((A)obj).MethodThatOnlyACanDo();
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T> class ListLogic { public: struct LogicBase {
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...
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...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.