Basically
Class A {...}
Class B : A {...}
Class C : B {...}
Class AGroup : Collection {...}
Class BGroup : AGroup {...}
Class CGroup : BGroup {...}
Class B : A {...}
Class C : B {...}
Class AGroup : Collection {...}
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
Matthew CochranPosted Jan 26, 2008, 10:17 AM
There are probably many ways to handle this... the one that comes to my mind first is to define a bunch of interfaces to define the collection behavior you want and then instead of building your objects directly through an inheritance chain, use composition.
public class A { }
(T vaue) where T : A;
(T value) where T : B;
(T value) where T : C;
public class B : A { }
public class C : B { }
public interface IAGroupActions
{
void DoStuffWithA
}
public interface IBGroupActions: IAGroupActions
{
void DoStuffWithB
}
public interface ICGroupActions: IBGroupActions
{
void DoStuffWithC
}
Then you just have build the AGroup, Build the BGroup and implement mostly wrapping the AGroup functionality and the CGroup would wrap a BGroup.
public class AGroup : Collection<A>, IAGroupActions
(T vaue) where T : A
(T value) where T : B
(T value) where T : A
{
#region IAGroupActions Members
public void DoStuffWithA
{
throw new NotImplementedException();
}
#endregion
}
public class BGroup : ICollection<B>, IBGroupActions
{
#region Member Variables
private AGroup m_core = new AGroup();
#endregion
#region IBGroupActions Members
public void DoStuffWithB
{
throw new NotImplementedException();
}
#endregion
#region IAGroupActions Members
public void DoStuffWithA
{
m_core.DoStuffWithA(value);
}
#endregion
#region ICollection Members
public void Add(B item)
{
// expose all properties and methods of the core like this
m_core.Add(item);
}
/* code removed to keep sample short */
}
I'm not sure if this gets you where you need to go... There may be a more elegant way to do this that I'm just not thinking of, but if there is it would probably use a similiar approach.
-Matt
Tyler CopplePosted Jan 25, 2008, 6:27 PM
I need
1.AGroup to have A,B,C objects.
2.BGroup to have B,C objects.
3.CGroup to have C objects.
Also (here's where you missed it I think)
I need AGroup to inherit from Collection while defiining some other functions and members.
I need BGroup to inherit all the things from AGroup as well as have some of it's own.
I need CGroup to inherit all the thigns from AGroup and BGroup plus some of its' own.
So the line of objects uses inheritence, and the collections use inheritence, while remaining strongly typed.
I'm thinking the best way to this is not to inherit from collection but just to have a generic collection in AGroup, then BGroup and CGroup can work as they need too.
Does that make sense? I hope there's a solution I don't want to use CollectionBase.
Matthew CoxPosted Jan 25, 2008, 4:42 PM
concerning:
Class AGroup : Collection {...}
Do you actually intend to inherit from class Collection? as in the Collection class using generics?
Or did you mean that AGroup would inherit from an "actual" collection of type A
Because in the latter case .... that can't be done.
Seems like a stupid question but there have been instances where someone has meant something completely different than what they wrote. :)
Matthew CochranPosted Jan 25, 2008, 4:39 PM
public class A { }
public class B : A { }
public class C : B { }
You would not have to specify these collections as seperate classes as follows unless they have additional functionality beyond a basic Collection<>...
public class AGroup : Collection<A> { }
public class BGroup : Collection<B> { }
public class CGroup : Collection<C> { }
Instead... You can just use the generic collection type in your code
Collection<A> aCollection = new Collection<A>();
Collection<B> bCollection = new Collection<B>();
Collection<C> cCollection = new Collection<C>();
aCollection can hold A, B and C objects
bCollection can hold B, and C objects
cCollection can hold only C objects
hope this helps
-Matt