473,769 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Anim als>

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<Anim als> and
that is inherited from Collection<T> so that I can define some common
routines in Collection<Anim als>. Is there a way to do that?

Thanks,

Chuck Cobb
Mar 23 '06 #1
4 1420
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<Cat s> and Collection<Dogs > inherited from Collection<Anim als> and
that is inherited from Collection<T> so that I can define some common
routines in Collection<Anim als>. Is there a way to do that?


No, but how about

class AnimalCollectio n<T> : Collection<T> where T : Animal {}
class DogCollection : AnimalCollectio n<Dog> {}
class CatCollection : AnimalCollectio n<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.o rg> wrote in message
news:eo******** ******@tk2msftn gp13.phx.gbl...
Chuck,
What I would like to be able to do is have
Collection<Ca ts> and Collection<Dogs > inherited from Collection<Anim als>
and
that is inherited from Collection<T> so that I can define some common
routines in Collection<Anim als>. Is there a way to do that?


No, but how about

class AnimalCollectio n<T> : Collection<T> where T : Animal {}
class DogCollection : AnimalCollectio n<Dog> {}
class CatCollection : AnimalCollectio n<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
- INotifyProperty Changed
- IEditableObject

- Class ClientList defines a collection of ClientInfo objects
- ClientList is Inherited from BaseList that defines some basic list
methods and interfaces such as:
- IBindingListVie w
- IRaiseItemChang edEvents
- 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<BaseIn fo> // a
general control that supports all business objects, and

CtlClients : CtlDataGrid<Cli entList> // 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<BaseIn fo> in order to
use it as parameter 'T' in the generic type or method CtlDataGrid".

ClientList is inherited from BaseList<Client Info> and ClientInfo is
inherited from BaseInfo so I'm surprised that the compiler is not able to
convert ClientList into BaseList<BaseIn fo>. 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.o rg> wrote in message
news:eo******** ******@tk2msftn gp13.phx.gbl...
Chuck,
What I would like to be able to do is have
Collection<Ca ts> and Collection<Dogs > inherited from Collection<Anim als>
and
that is inherited from Collection<T> so that I can define some common
routines in Collection<Anim als>. Is there a way to do that?


No, but how about

class AnimalCollectio n<T> : Collection<T> where T : Animal {}
class DogCollection : AnimalCollectio n<Dog> {}
class CatCollection : AnimalCollectio n<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
1671
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 Int32ArrayList = System.GCollections.ArrayList<Int32>; .... Int32ArrayList myArrayList = new Int32ArrayList();
16
1841
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); } public class B {
12
2744
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 it defines the language more completely. Great to use.
1
1893
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 = new List<IPerson>(); ... // get the persons return personList;
9
5986
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 Experience Only, Please. ...
11
2483
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>> _queries; where DLinqQuery is a generic class that takes a type parameter
3
2606
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 generics i understand that one doesnt have to used these custom collections. So i want to move this 1.1 project to 2.0. I have been reading about generics but i geuss i dont really get hpw to implement them. For example i have 2 classees in the...
1
1677
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
1709
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 object. For each business object, I would have something like below: public class Apple { //apple stuff
4
4221
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 impression that windows.forms controls used EventHandlerLists because generally most events aren't consumed so this saves memory. I can add a button to a form, and have this.button9.Click += new System.EventHandler(this.button9_Click);...
0
9423
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,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
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 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...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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.