Creating Typesafe Collections
-
Hi, I'm just wondering whether I'm on the right way or not. I'd like to create a typesafe collection. The data should be held in an ArrayList for simplicity. For all my collection-classes I'd like to create a base class (say MyCollectionBase) which implements all functionality which is not related to the objects-types contained in this collection. For a start, I thought about something like this:
public class ColBase : IEnumerable { public ColBase() {} public IEnumerator GetEnumerator() { return mData.GetEnumerator(); } public virtual int Count() { return mData.Count; } public virtual void Clear() { mData.Clear(); } protected ArrayList mData = new ArrayList(); }
So far so good. Now the derived classes support the foreach syntax which is what I wanted. The derived classes have to implement their versions of Add(), AddRange(), Remove(), etc, like (say we have a class called Item which represents the individual items in the collection):public class Col : ColBase { public Col() {} public Item Add(Item item) { return (Item) base.mData[base.mData.Add(item)]; } public void Remove(Item item) { base.mData.Remove(item); } }
But my Baseclass does not support the [] indexer. How do I have to implement this? Am I on the right way or should this be done completely different? Thanks for comments... MatthiasIf eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)
-
Hi, I'm just wondering whether I'm on the right way or not. I'd like to create a typesafe collection. The data should be held in an ArrayList for simplicity. For all my collection-classes I'd like to create a base class (say MyCollectionBase) which implements all functionality which is not related to the objects-types contained in this collection. For a start, I thought about something like this:
public class ColBase : IEnumerable { public ColBase() {} public IEnumerator GetEnumerator() { return mData.GetEnumerator(); } public virtual int Count() { return mData.Count; } public virtual void Clear() { mData.Clear(); } protected ArrayList mData = new ArrayList(); }
So far so good. Now the derived classes support the foreach syntax which is what I wanted. The derived classes have to implement their versions of Add(), AddRange(), Remove(), etc, like (say we have a class called Item which represents the individual items in the collection):public class Col : ColBase { public Col() {} public Item Add(Item item) { return (Item) base.mData[base.mData.Add(item)]; } public void Remove(Item item) { base.mData.Remove(item); } }
But my Baseclass does not support the [] indexer. How do I have to implement this? Am I on the right way or should this be done completely different? Thanks for comments... MatthiasIf eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)
Take a look at the
CollectionBase
. It already has two properties - both protected - to access the underlyingArrayList
. It also hasOn*
methods defined that will be called when you use theList
property, but they will not be called when you use theInnerList
property. This is handy if you want to define events for when the collection changes. It's a good starting point for a typed collection, since you can define the methods that take a typed object (whatever type you're collecting) and call the explicit interface methods (likeIList.Add
), so that it appears as a typed collection. Explicit interface methods are how many classes in the .NET Framework Class Library - including classes inSystem.Data.*
andSystem.Collections.*
- appear to take a specific type as a parameter even though they implement interfaces that must access anobject
.Microsoft MVP, Visual C# My Articles
-
Hi, I'm just wondering whether I'm on the right way or not. I'd like to create a typesafe collection. The data should be held in an ArrayList for simplicity. For all my collection-classes I'd like to create a base class (say MyCollectionBase) which implements all functionality which is not related to the objects-types contained in this collection. For a start, I thought about something like this:
public class ColBase : IEnumerable { public ColBase() {} public IEnumerator GetEnumerator() { return mData.GetEnumerator(); } public virtual int Count() { return mData.Count; } public virtual void Clear() { mData.Clear(); } protected ArrayList mData = new ArrayList(); }
So far so good. Now the derived classes support the foreach syntax which is what I wanted. The derived classes have to implement their versions of Add(), AddRange(), Remove(), etc, like (say we have a class called Item which represents the individual items in the collection):public class Col : ColBase { public Col() {} public Item Add(Item item) { return (Item) base.mData[base.mData.Add(item)]; } public void Remove(Item item) { base.mData.Remove(item); } }
But my Baseclass does not support the [] indexer. How do I have to implement this? Am I on the right way or should this be done completely different? Thanks for comments... MatthiasIf eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)
-
Hi, I'm just wondering whether I'm on the right way or not. I'd like to create a typesafe collection. The data should be held in an ArrayList for simplicity. For all my collection-classes I'd like to create a base class (say MyCollectionBase) which implements all functionality which is not related to the objects-types contained in this collection. For a start, I thought about something like this:
public class ColBase : IEnumerable { public ColBase() {} public IEnumerator GetEnumerator() { return mData.GetEnumerator(); } public virtual int Count() { return mData.Count; } public virtual void Clear() { mData.Clear(); } protected ArrayList mData = new ArrayList(); }
So far so good. Now the derived classes support the foreach syntax which is what I wanted. The derived classes have to implement their versions of Add(), AddRange(), Remove(), etc, like (say we have a class called Item which represents the individual items in the collection):public class Col : ColBase { public Col() {} public Item Add(Item item) { return (Item) base.mData[base.mData.Add(item)]; } public void Remove(Item item) { base.mData.Remove(item); } }
But my Baseclass does not support the [] indexer. How do I have to implement this? Am I on the right way or should this be done completely different? Thanks for comments... MatthiasIf eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)
I found really nice collection generator: http://kristopherjohnson.net/cgi-bin/twiki/view/KJ/TypedCollectionGenerator it works pretty well, just install it as a tool in VS and you can have typed collection in a second.