Home All Groups Group Topic Archive Search About

Creating a Collection Class

Author
4 Sep 2006 8:04 AM
APA
I already have a colleciton class that implements IEnumerable but I also what to be able to
implement this inteface: MyCollectionClass["somestringkey"].

EX:

    //MyCollectionClass is s collection of MyObject objects
    MyObject mo = MyCollectionClass["somestringkey"];

     or

    string mypropertryvalue = MyCollectionClass["somestringkey"].SomeProperty


How do I implement the string key indexer in my collection class?



Thx.

Author
4 Sep 2006 8:17 AM
Joanna Carter [TeamB]
"APA" <buddy***@hotmail.com> a écrit dans le message de news:
O8$Gui$zGHA.4***@TK2MSFTNGP03.phx.gbl...

| How do I implement the string key indexer in my collection class?

In .NET 1.1, use a HashTable, in .NET 2.0 use Dictionary<string, MyObject>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Author
4 Sep 2006 8:34 AM
Morten Wennevik
Hi APA

You can do this by adding a this-property to your class

     public MyObject this[string key]
     {
         get
      {
        // look up the value
         }
         set
      {
        // set the value
      }
     }


On Mon, 04 Sep 2006 10:04:01 +0200, APA <buddy***@hotmail.com> wrote:

Show quote
> I already have a colleciton class that implements IEnumerable but I al=
so  =

> what to be able to implement this inteface:  =

> MyCollectionClass["somestringkey"].
>
> EX:
>
>     //MyCollectionClass is s collection of MyObject objects
>     MyObject mo =3D MyCollectionClass["somestringkey"];
>
>      or
>
>     string mypropertryvalue =3D  =

> MyCollectionClass["somestringkey"].SomeProperty
>
>
> How do I implement the string key indexer in my collection class?
>
>
>
> Thx.
>
>



-- =

Happy Coding!
Morten Wennevik [C# MVP]
Author
4 Sep 2006 6:43 PM
sloan
In 1.1, you most often subclass CollectionBase.

Morten is correct, you want an indexer.

     public MyObject this[string key]
     {
         get
  {
// look up the value
         }
         set
  {
// set the value
  }
     }

You can overload it also

     public MyObject this[int idx]
     {
         get { return this.InnerList[idx];}//this is how you might do it
when I inherit from CollectionBase
  {
// look up the value
         }
         set
  {
// set the value
  }
     }

2.0 .. you will "switch out" to Generics usually.

spaces.msn.com/sholliday/

Find a referenece I make to Ludwig, and you'll find a nice article on
swapping out to generics, if you're at a 2.0 level.



"APA" <buddy***@hotmail.com> wrote in message
news:O8$Gui$zGHA.4972@TK2MSFTNGP03.phx.gbl...
> I already have a colleciton class that implements IEnumerable but I also
what to be able to
Show quote
> implement this inteface: MyCollectionClass["somestringkey"].
>
> EX:
>
> //MyCollectionClass is s collection of MyObject objects
> MyObject mo = MyCollectionClass["somestringkey"];
>
>      or
>
> string mypropertryvalue = MyCollectionClass["somestringkey"].SomeProperty
>
>
> How do I implement the string key indexer in my collection class?
>
>
>
> Thx.
>
>

AddThis Social Bookmark Button