Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generic Collections & Inheritence Question

Status
Not open for further replies.

TCopple

Programmer
Jun 5, 2007
4
US
I'm designing an object model which needs a few constraints and I'm not sure how exactly to do what I want in C#, any help is appreciated.

Basically
Code:
Class A {...}
Class B : A {...}
Class C : B {...}
Class AGroup : Collection<A> {...}
Class BGroup : AGroup {...}
Class CGroup : BGroup {...}

The feature I need is to make sure that BGroup's collection only has B objects in it, but retains all the methods and members from AGroup.

While CGroups's collection only has C objects in it but has both A and B methods and members accessible.

Strong typing which gets stronger each level of inheritance.

Is this possible?
Thanks in advance,
Tyler Copple
 
how about using generic typing:

Code:
Class A {...}
Class B : A {...}
Class C : B {...}

Class AGroup<T> : Collection<T> where T : A {...}
Class BGroup<T> : Collection<T> where T : B {...}

//...

AGroup<B> groupOfB = new AGroup<B>();
BGroup<C> groupOfC = new AGroup<C>();

this way you know that all objects in AGroup are of type A, and likewise BGroup.



mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top