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

Question on Generics

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
Mar 23 '06 #1
4 1398
What common routines do you want to define?

Mar 23 '06 #2
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 23 '06 #3
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.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eo**************@tk2msftngp13.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 23 '06 #4
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

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eo**************@tk2msftngp13.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 23 '06 #5

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

Similar topics

5
by: Matthew W. Jackson | last post by:
I had a question about the "using" statement and Generics in the next version of C#, and I was directed to this newsgroup. My question is: Will the following syntax be valid? using...
16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
3
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
14
by: cwineman | last post by:
Hello, I'm hoping to do something using Generics, but I'm not sure it's possible. Let's say I want to have a bunch of business objects and a data access class cooresponding to each business...
4
by: DeveloperX | last post by:
I'm having a play with EventHandlerList but the documentation is a bit ropey and I can't find any decent examples. It also doesn't seem to do what I was led to believe it would. I was under the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.