Hi!
I have a generic abstract base class MyCollection<Tw hich represents a collection
of T's with some common utility methods.
I then have a number of non-generic subclasses, e.g.
class FooCollection : MyCollection<Fo o>
{ /* utility methods only for foo collections */ }
class BarCollection : MyCollection<Ba r>
{ /* utility methods only for Bar collections */ }
Now, I'd like to provide a subset method in MyCollection - e.g. for getting
the first 10 items
(In real life selecting the subset is more complex and based on the structure
of MyCollection as well as properties of the T base type)
FooCollection allFoos = ...
FooCollection tenFoos = allFoos.GetSubs et(10);
But I'd like to return a FooCollection - not a MyCollection<Fo o>. The best
I have been able to come up is this method in MyCollection:
DERIVEDCOLLECTI ON GetSubset<DERIV EDCOLLECTION>(i nt count) where DERIVEDCOLLECTI ON:
MyCollection<T> , new()
{
DERIVEDCOLLECTI ON result = new DERIVEDCOLLECTI ON();
for(int i = 0; i < count; i++) // example logic only!
result.Add(this[i]);
return result;
}
This is not very elegant, since it requires the user to specify the target
class:
FooCollection tenFoos = allFoos.GetSubs et<FooCollectio n>(10);
An alternative would be to return an IEnumerable<T>, but that would require
the user to write
FooCollection allFoos = ...
FooCollection tenFoos = new FooCollection() ;
tenFoos.AddAll( allFoos.GetSubs et(10))
Or, finally, I could implement new GetSubset in all derived collections -
but since there are a number of such functions, that's not too useful either.
Does anybody have any suggestions? 1 1671
One possibility, add a second type to the generic parameters:
class FooCollection : MyCollection<Fo o, FooCollection>
{}
where MyCollection is now something like:
class MyCollection<T, Swhere S : new()
{
public S GetSubset(int count)
{
S newCollection = new S();
for (int i = 0; i < count; i++)
{
newCollection.A dd(this[i]);
}
return newCollection;
}
}
This would then simplify the callers syntax to simply:
FooCollection tenFoos = allFoos.GetSubs et(10);
Which was what you wanted correct?
This comes with an assumption: that each of your subclasses has a default
constructor (presumably to create an empty collection).
--
Adam Clauss
"Niels Ull" <ni******@hotma il.comwrote in message
news:97******** *************** ***@msnews.micr osoft.com...
Hi!
I have a generic abstract base class MyCollection<Tw hich represents a
collection of T's with some common utility methods. I then have a number
of non-generic subclasses, e.g.
class FooCollection : MyCollection<Fo o>
{ /* utility methods only for foo collections */ }
class BarCollection : MyCollection<Ba r>
{ /* utility methods only for Bar collections */ }
Now, I'd like to provide a subset method in MyCollection - e.g. for
getting the first 10 items
(In real life selecting the subset is more complex and based on the
structure of MyCollection as well as properties of the T base type)
FooCollection allFoos = ...
FooCollection tenFoos = allFoos.GetSubs et(10);
But I'd like to return a FooCollection - not a MyCollection<Fo o>. The best
I have been able to come up is this method in MyCollection:
DERIVEDCOLLECTI ON GetSubset<DERIV EDCOLLECTION>(i nt count) where
DERIVEDCOLLECTI ON: MyCollection<T> , new()
{
DERIVEDCOLLECTI ON result = new DERIVEDCOLLECTI ON();
for(int i = 0; i < count; i++) // example logic only!
result.Add(this[i]);
return result;
}
This is not very elegant, since it requires the user to specify the target
class: FooCollection tenFoos = allFoos.GetSubs et<FooCollectio n>(10);
An alternative would be to return an IEnumerable<T>, but that would
require the user to write
FooCollection allFoos = ...
FooCollection tenFoos = new FooCollection() ;
tenFoos.AddAll( allFoos.GetSubs et(10))
Or, finally, I could implement new GetSubset in all derived collections -
but since there are a number of such functions, that's not too useful
either.
Does anybody have any suggestions?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lawrence |
last post by:
I'm very unhappy with the error message that I'm giving in this
method:
/**
* 11-23-03 - getter
*
* We want to run the query against a MySql database and get back a
|
by: Carlos Ribeiro |
last post by:
Hello all,
I'm posting this to the list with the intention to form a group of
people interested in this type of solution. I'm not going to spam the
list with it, unless for occasional and relevant announcements. If
you're interested, drop me a note. But if for some reason you think
that this discussion is fine here at the c.l.py, please let me know.
** LONG POST AHEAD **
|
by: Max M |
last post by:
# -*- coding: latin-1 -*-
"""
I subclass datetime and timedelta
>>> dt = myDatetime(1970,1,1)
>>> type(dt)
<class 'dtime.myDatetime'>
|
by: Angelos Karantzalis |
last post by:
Hi guys. I've come across a problem when I tried to serialize a class into
xml, only to discover that the parent class's XML Serialization properties
weren't included in the output xml.
Actually, the class I'm serializing is two steps down in the inheritance
ladder. It's got a parent class which also has a parent class :( All those
classes in the hierarchy are Xml Serializable, and I'd think that it should
be obvious that all...
|
by: Jeroen Smits |
last post by:
To Microsoft or other people who are interested, I have a feature request:
I would really like to have an option to 'hide' a property or method from an
inherited class. For example when I create a subclass and supply a default
value for a property. Currently I create a new property, using the new
keyword, and only supply the get accessor witch will always return the
predefined value. But I would really like to be able to hide it...
| |
by: Keith Patrick |
last post by:
Could someone tell me if it's possible (and if so, how) to call an
explicitly-implemented interface method from a subclass? I have a class in
which I have to explicity implement some methods, but my subclasses, which
should use them, cannot call them. I'm not sure if it just isn't possible
due to visibility or if I just don't have the syntax right. I can't make
the methods protected, as the compiler complains (although the documentation...
|
by: Dan |
last post by:
I have class B and C which inherit from class A.
I have a static method:
A* aRequest(unsigned char *byte_buffer, size_t length)
{
A *foo;
if(something == true)
{
foo = new B;
|
by: s.lipnevich |
last post by:
Hi All,
Is anything wrong with the following code?
class Superclass(object):
def __new__(cls):
# Questioning the statement below
return super(Superclass, cls).__new__(Subclass)
class Subclass(Superclass):
pass
|
by: damacy |
last post by:
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.
say, there are 4 machines present in the network; , , and
and if i run my program on , it should be able to find "host names"
and "ip addresses" of the other machines; , and ?
i have read some threads posted on this group, however, they only work
for localhost, not the entire network.
|
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,...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |