Question on Generics

C

Chuck Cobb

I have a question regarding generics: Suppose I want to create some generic
collection classes:

Collection<Cats> c;
Collection<Dogs> d;

and both Cats and Dogs are inherited from a base class called Animals. I'd
like to have a class:

Collection<Animals>

that has all the common routines for collections of Cats and Dogs defined.
How do I do that?

The problem I'm having trouble understanding is this...both Collection<Cats>
and Collection<Dogs> are inherited from Collection<T> where T is just a type
identifier (not a real type). What I would like to be able to do is have
Collection<Cats> and Collection<Dogs> inherited from Collection<Animals> and
that is inherited from Collection<T> so that I can define some common
routines in Collection<Animals>. Is there a way to do that?

Thanks,

Chuck Cobb
 
M

Mattias Sjögren

Chuck,
What I would like to be able to do is have
Collection<Cats> and Collection<Dogs> inherited from Collection<Animals> and
that is inherited from Collection<T> so that I can define some common
routines in Collection<Animals>. Is there a way to do that?

No, but how about

class AnimalCollection<T> : Collection<T> where T : Animal {}
class DogCollection : AnimalCollection<Dog> {}
class CatCollection : AnimalCollection<Cat> {}


Mattias
 
C

Chuck Cobb

That looks like the solution!!! Thanks Mattias!!

The trick is adding that "where T : Animal" clause to the end... I wasn't
aware of that syntax. I think that will solve the problem.
 
C

Chuck Cobb

Here's one more question on this topic:

I am developing a CRM system using business objects...I have used the
approach we previously discussed to develop lists of business objects for
the application and it works great! For example,


- Class ClientInfo defines a business object for a Client
- ClientInfo is inherited from BaseInfo which defines some basic item
methods and interfaces such as
- INotifyPropertyChanged
- IEditableObject

- Class ClientList defines a collection of ClientInfo objects
- ClientList is Inherited from BaseList that defines some basic list
methods and interfaces such as:
- IBindingListView
- IRaiseItemChangedEvents
- IEditableObject

The problem I'm trying to resolve now has to do with Control binding...At
the moment, I have individual controls and datagrids bound directly to each
collection of business objects. For example, CtlClients has a datagrid that
is bound to the ClientList collection above and other business objects are
bound to other controls.

I would also like to use generics in the controls so that I can move some of
the common routines into a base control that the other controls inherit
from. What I would like to have is something like this:

CtlDataGrid<T> : CtlBase where T : BaseList<BaseInfo> // a
general control that supports all business objects, and

CtlClients : CtlDataGrid<ClientList> // a specific control
for clients that is inherited from the above

When I define these controls in this way, I get an error message that says
"The type ClientList must be convertible to BaseList<BaseInfo> in order to
use it as parameter 'T' in the generic type or method CtlDataGrid".

ClientList is inherited from BaseList<ClientInfo> and ClientInfo is
inherited from BaseInfo so I'm surprised that the compiler is not able to
convert ClientList into BaseList<BaseInfo>. Do I need to write a custom
TypeConverter to accomplish that? I attempted to do that, but it didn't
seem to make any difference...

Thanks,

Chuck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top