Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating Typesafe Collections

Creating Typesafe Collections

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    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... Matthias

    If 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)

    www.emvoid.de

    H G W 3 Replies Last reply
    0
    • M matthias s 0

      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... Matthias

      If 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)

      www.emvoid.de

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Take a look at the CollectionBase. It already has two properties - both protected - to access the underlying ArrayList. It also has On* methods defined that will be called when you use the List property, but they will not be called when you use the InnerList 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 (like IList.Add), so that it appears as a typed collection. Explicit interface methods are how many classes in the .NET Framework Class Library - including classes in System.Data.* and System.Collections.* - appear to take a specific type as a parameter even though they implement interfaces that must access an object.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • M matthias s 0

        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... Matthias

        If 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)

        www.emvoid.de

        G Offline
        G Offline
        gokselm
        wrote on last edited by
        #3

        After you inherit from an interface (ex:IList) that has the indexer try the following. new public ColBase this[int index] { get { return (ColBase ) base[index]; } set { base[index] = value; } }

        1 Reply Last reply
        0
        • M matthias s 0

          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... Matthias

          If 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)

          www.emvoid.de

          W Offline
          W Offline
          Werdna
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups