Indexers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvin Bruney

    #1

    Indexers

    In later versions of the framework is it possible to have indexers on
    properties for member fields?

    Say for example I have

    private ArrayList blah;

    and a propget

    public object Blah[int index]
    {
    get { return blah[index]; }
    }



  • Mattias Sjögren

    #2
    Re: Indexers

    Alvin,
    [color=blue]
    >In later versions of the framework is it possible to have indexers on
    >properties for member fields?[/color]

    No. But this is a C# language limitation.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Alvin Bruney

      #3
      Re: Indexers

      Will it ever be added or is there a good reason for not having this?

      Since propertys are methods anyway, I dont see a technicaly reason why they
      cannot be permited

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:OwKasqQtDH A.1224@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Alvin,
      >[color=green]
      > >In later versions of the framework is it possible to have indexers on
      > >properties for member fields?[/color]
      >
      > No. But this is a C# language limitation.
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/
      > Please reply only to the newsgroup.[/color]


      Comment

      • Mattias Sjögren

        #4
        Re: Indexers

        Alvin,
        [color=blue]
        >Will it ever be added or is there a good reason for not having this?[/color]

        "Ever" is a long time, and since nothing beyond the Whidbey release
        has been announced, I don't thing anyone can tell. It probably also
        depends on customer demand.

        [color=blue]
        >Since propertys are methods anyway, I dont see a technicaly reason why they
        >cannot be permited[/color]

        While they technically are implemented that way, I think conceptually
        properties and methods are very different.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org

        Please reply only to the newsgroup.

        Comment

        • Michael Lang

          #5
          Re: Indexers

          "Alvin Bruney" <alvin.bruney@. telia.com.> wrote in news:O8vc$dQtDH A.2408
          @tk2msftngp13.p hx.gbl:
          [color=blue]
          > In later versions of the framework is it possible to have indexers on
          > properties for member fields?
          >
          > Say for example I have
          >
          > private ArrayList blah;
          >
          > and a propget
          >
          > public object Blah[int index]
          > {
          > get { return blah[index]; }
          > }[/color]

          There is only one thing wrong with the indexer in your code snippet. You
          forgot the "this" keyword. Here is a snippet from one of my custom
          collection classes...

          public class EntityCollectio n:CollectionBas e
          {
          // "this.List" is an ArrayList that is a member of CollectionBase
          public Entity this[int index]
          {
          get { return (Entity)this.Li st[index]; }
          }
          //...<Snip>
          }

          To access a member of this collection...

          EntityCollectio n ec = new EntityCollectio n();
          ec.Add(...);
          Entity e1 = ec[0];

          If you want to access a single element from a member collection, then you
          should be calling the indexer on that member property... IE.

          //A collection of the "Document" class
          public class DocumentCollect ion:CollectionB ase{...}
          public class Application
          {
          private DocumentCollect ion _docs;
          public Application(){_ docs=new DocumentCollect ion();}
          public DocumentCollect ion Documents{get{r eturn _docs;}}
          }

          to access a document of the application...

          Application app = new Application();
          ... add some documents here...
          Document doc = app.Documents[i];

          Let me know if you have further questions.

          To see how to create a collection like the ones above, use my open source
          collection generator/templates found at:
          http://sourceforge.net/projects/colcodegen

          Michael Lang, MCSD

          Comment

          • Alvin Bruney

            #6
            Re: Indexers

            yes but that means I have to refer to it as the class, I could have many
            different arrays in a class and I want to use an indexer on more than one.

            Methods are the only way it seems.

            I was looking for somethin glike a property and an indexer for that kind of
            scenario (yes i know properties are just methods anyway)

            "Michael Lang" <ml@nospam.co m> wrote in message
            news:Xns944692E E1FF66lang1474i cqmailcom@207.4 6.248.16...[color=blue]
            > "Alvin Bruney" <alvin.bruney@. telia.com.> wrote in news:O8vc$dQtDH A.2408
            > @tk2msftngp13.p hx.gbl:
            >[color=green]
            > > In later versions of the framework is it possible to have indexers on
            > > properties for member fields?
            > >
            > > Say for example I have
            > >
            > > private ArrayList blah;
            > >
            > > and a propget
            > >
            > > public object Blah[int index]
            > > {
            > > get { return blah[index]; }
            > > }[/color]
            >
            > There is only one thing wrong with the indexer in your code snippet. You
            > forgot the "this" keyword. Here is a snippet from one of my custom
            > collection classes...
            >
            > public class EntityCollectio n:CollectionBas e
            > {
            > // "this.List" is an ArrayList that is a member of CollectionBase
            > public Entity this[int index]
            > {
            > get { return (Entity)this.Li st[index]; }
            > }
            > //...<Snip>
            > }
            >
            > To access a member of this collection...
            >
            > EntityCollectio n ec = new EntityCollectio n();
            > ec.Add(...);
            > Entity e1 = ec[0];
            >
            > If you want to access a single element from a member collection, then you
            > should be calling the indexer on that member property... IE.
            >
            > //A collection of the "Document" class
            > public class DocumentCollect ion:CollectionB ase{...}
            > public class Application
            > {
            > private DocumentCollect ion _docs;
            > public Application(){_ docs=new DocumentCollect ion();}
            > public DocumentCollect ion Documents{get{r eturn _docs;}}
            > }
            >
            > to access a document of the application...
            >
            > Application app = new Application();
            > ... add some documents here...
            > Document doc = app.Documents[i];
            >
            > Let me know if you have further questions.
            >
            > To see how to create a collection like the ones above, use my open source
            > collection generator/templates found at:
            > http://sourceforge.net/projects/colcodegen
            >
            > Michael Lang, MCSD[/color]


            Comment

            • Michael Lang

              #7
              Re: Indexers

              "Alvin Bruney" <alvin.bruney@. telia..com.> wrote in
              news:OSdHSCeuDH A.2544@TK2MSFTN GP09.phx.gbl:
              [color=blue]
              > yes but that means I have to refer to it as the class, I could have
              > many different arrays in a class and I want to use an indexer on more
              > than one.
              >
              > Methods are the only way it seems.
              >
              > I was looking for somethin glike a property and an indexer for that
              > kind of scenario (yes i know properties are just methods anyway)
              >
              > "Michael Lang" <ml@nospam.co m> wrote in message
              > news:Xns944692E E1FF66lang1474i cqmailcom@207.4 6.248.16...[color=green]
              >>
              >> If you want to access a single element from a member collection, then
              >> you should be calling the indexer on that member property... IE.
              >>
              >> //A collection of the "Document" class
              >> public class DocumentCollect ion:CollectionB ase{...}
              >> public class Application
              >> {
              >> private DocumentCollect ion _docs;
              >> public Application(){_ docs=new DocumentCollect ion();}
              >> public DocumentCollect ion Documents{get{r eturn _docs;}}
              >> }
              >>
              >> to access a document of the application...
              >>
              >> Application app = new Application();
              >> ... add some documents here...
              >> Document doc = app.Documents[i];[/color][/color]

              You can always make the ArrayList public instead of private, or create a public property
              that returns that arrayList or a clone of it. If you need more control, or don't want
              to waste resources making clones, then I still recommend my previous Documents sample.
              Here is a example of making the members public:

              =============== =============== =============== =============== ==========
              using System;
              using System.Collecti ons;

              namespace IndexerTest2
              {
              public class MyClass
              {
              public ArrayList Array1; //publically editable
              private ArrayList _array2;

              public MyClass()
              {
              Array1 = new ArrayList();
              _array2 = new ArrayList();
              _array2.Add("in ternal add test");
              }
              public ArrayList Array2
              { //if you don't want it to be editable return a clone.
              get{return (ArrayList)_arr ay2.Clone();}
              }

              [STAThread]
              static void Main(string[] args)
              { //Testing code...
              MyClass c1 = new MyClass();
              c1.Array1.Add(" test 1");
              Console.WriteLi ne(c1.Array1[0].ToString());
              c1.Array2.Add(" test 2"); //does not add to _array2...
              Console.WriteLi ne(c1.Array2[0].ToString());
              Console.WriteLi ne(c1.Array2[1].ToString()); //this line fails
              Console.ReadLin e();
              }
              }
              }
              =============== =============== =============== =============== ==========

              Comment

              • Matthew W. Jackson

                #8
                Re: Indexers

                You can do it, but it's more than just an indexer. Basically, you have a
                method that returns an ICollection for each of your internal Array lists.
                You can write your own private class that implements ICollection and return
                it, in case you don't want to allow certain operations, such as Insert,
                Remove, Set, etc. The point is, you can control access to your internal
                array list, and even strongly type the data.

                For a good example, look at how Hashtable lets you access Keys and Values.
                There are two properties that both return ICollection that basically give
                you two different views of the same data structure. As a result, you can do
                hashtable.Keys[0] and hashtable.Items[0]. Even better, you can use
                foreach() on these properties.

                Here's a skeleton of how you'd do it:

                public class MultiCollection {

                private SubList list1;
                private SubList list2;
                private SubList list3;
                // so on...

                private class SubList : ICollection {

                internal ArrayList list;

                // implement all other required members, throwing exceptions on
                unsupported operations

                }

                public ICollection Indexer1 {
                get {
                return list1;
                }
                }

                public ICollection Indexer2 {
                get {
                return list2;
                }
                }

                // so on...

                }

                Whether you decide to put the list logic in your private SubList class or in
                the container class is up to you. It depends on what you're doing. In the
                case of the Hashtable, the sub-lists (Keys and Values) basically hold a
                pointer to the parent hashtable and access it's private data directly.


                --Matthew W. Jackson


                "Alvin Bruney" <alvin.bruney@. telia..com.> wrote in message
                news:OSdHSCeuDH A.2544@TK2MSFTN GP09.phx.gbl...[color=blue]
                > yes but that means I have to refer to it as the class, I could have many
                > different arrays in a class and I want to use an indexer on more than one.
                >
                > Methods are the only way it seems.
                >
                > I was looking for somethin glike a property and an indexer for that kind[/color]
                of[color=blue]
                > scenario (yes i know properties are just methods anyway)
                >
                > "Michael Lang" <ml@nospam.co m> wrote in message
                > news:Xns944692E E1FF66lang1474i cqmailcom@207.4 6.248.16...[color=green]
                > > "Alvin Bruney" <alvin.bruney@. telia.com.> wrote in news:O8vc$dQtDH A.2408
                > > @tk2msftngp13.p hx.gbl:
                > >[color=darkred]
                > > > In later versions of the framework is it possible to have indexers on
                > > > properties for member fields?
                > > >
                > > > Say for example I have
                > > >
                > > > private ArrayList blah;
                > > >
                > > > and a propget
                > > >
                > > > public object Blah[int index]
                > > > {
                > > > get { return blah[index]; }
                > > > }[/color]
                > >
                > > There is only one thing wrong with the indexer in your code snippet.[/color][/color]
                You[color=blue][color=green]
                > > forgot the "this" keyword. Here is a snippet from one of my custom
                > > collection classes...
                > >
                > > public class EntityCollectio n:CollectionBas e
                > > {
                > > // "this.List" is an ArrayList that is a member of CollectionBase
                > > public Entity this[int index]
                > > {
                > > get { return (Entity)this.Li st[index]; }
                > > }
                > > //...<Snip>
                > > }
                > >
                > > To access a member of this collection...
                > >
                > > EntityCollectio n ec = new EntityCollectio n();
                > > ec.Add(...);
                > > Entity e1 = ec[0];
                > >
                > > If you want to access a single element from a member collection, then[/color][/color]
                you[color=blue][color=green]
                > > should be calling the indexer on that member property... IE.
                > >
                > > //A collection of the "Document" class
                > > public class DocumentCollect ion:CollectionB ase{...}
                > > public class Application
                > > {
                > > private DocumentCollect ion _docs;
                > > public Application(){_ docs=new DocumentCollect ion();}
                > > public DocumentCollect ion Documents{get{r eturn _docs;}}
                > > }
                > >
                > > to access a document of the application...
                > >
                > > Application app = new Application();
                > > ... add some documents here...
                > > Document doc = app.Documents[i];
                > >
                > > Let me know if you have further questions.
                > >
                > > To see how to create a collection like the ones above, use my open[/color][/color]
                source[color=blue][color=green]
                > > collection generator/templates found at:
                > > http://sourceforge.net/projects/colcodegen
                > >
                > > Michael Lang, MCSD[/color]
                >
                >[/color]


                Comment

                • Michael Lang

                  #9
                  Re: Indexers

                  "Matthew W. Jackson" <themuujAThotma ilDOTcom@NOSPAM .NOSPAM> wrote in
                  news:Kstzb.9571 7$Vs1.64031@twi ster.austin.rr. com:
                  [color=blue]
                  > You can do it, but it's more than just an indexer. Basically, you
                  > have a method that returns an ICollection for each of your internal
                  > Array lists. You can write your own private class that implements
                  > ICollection and return it, in case you don't want to allow certain
                  > operations, such as Insert, Remove, Set, etc. The point is, you can
                  > control access to your internal array list, and even strongly type the
                  > data.
                  >
                  > For a good example, look at how Hashtable lets you access Keys and
                  > Values. There are two properties that both return ICollection that
                  > basically give you two different views of the same data structure. As
                  > a result, you can do hashtable.Keys[0] and hashtable.Items[0]. Even
                  > better, you can use foreach() on these properties.[/color]

                  Your general idea is correct. But, I'm guessing you meant to say:

                  "Basically, you have a property that returns an ICollection for each of
                  your internal Array lists." (swapped method out for property).

                  The "Keys" and the "Values" members of hashtable are properties, not
                  methods. If they were methods they would have parenthesis when you
                  reference them, IE:

                  hashtable.Keys( )[0] and hashtable.Value s()[0]

                  Also note there is no "Items" property. The "Values" property holds the
                  hashtable's values. The "Item" (no s), is the property that is also the
                  indexer for the hashtable, which really just returns items from the
                  "Values" property of the hashtable.

                  Kudos on the excellent example. My "documents" example (same idea)
                  propably should have exposed a second and third sub list property to be as
                  clear as yours.

                  Michael Lang, MCSD

                  Comment

                  • Michael Lang

                    #10
                    Re: Indexers

                    Michael Lang <ml@nospam.co m> wrote in
                    news:Xns94469C9 5822B8lang1474i cqmailcom@207.4 6.248.16:
                    [color=blue]
                    > "Alvin Bruney" <alvin.bruney@. telia..com.> wrote in
                    > news:OSdHSCeuDH A.2544@TK2MSFTN GP09.phx.gbl:
                    >[color=green]
                    >> yes but that means I have to refer to it as the class, I could have
                    >> many different arrays in a class and I want to use an indexer on more
                    >> than one.
                    >> <snip>
                    >>
                    >> "Michael Lang" <ml@nospam.co m> wrote in message
                    >> news:Xns944692E E1FF66lang1474i cqmailcom@207.4 6.248.16...[color=darkred]
                    >>>
                    >>> <snip>
                    >>>
                    >>> //A collection of the "Document" class
                    >>> public class DocumentCollect ion:CollectionB ase{...}
                    >>> public class Application
                    >>> {
                    >>> private DocumentCollect ion _docs;
                    >>> public Application(){_ docs=new DocumentCollect ion();}
                    >>> public DocumentCollect ion Documents{get{r eturn _docs;}}
                    >>> }
                    >>>
                    >>> to access a document of the application...
                    >>>
                    >>> Application app = new Application();
                    >>> ... add some documents here...
                    >>> Document doc = app.Documents[i];[/color][/color][/color]

                    Let me be a little more clear...

                    public DocumentCollect ion Documents{get{r eturn _docs;}}

                    In the line above "Documents" is the property name.
                    "DocumentCollec tion" is the type of the property. I could have many
                    properties that all return a "DocumentCollec tion" with different names...

                    public DocumentCollect ion Documents{get{r eturn _docs;}}
                    public DocumentCollect ion Papers{get{retu rn _docs;}}
                    public DocumentCollect ion Reports{get{ret urn _docs;}}

                    Your return type could just as easily by "ArrayList" instead of
                    "DocumentCollec tion". As Matthew also noted, you may want to create a
                    custom collection type instead of returning the raw array. This way you
                    can add extra validation to limit the types of operations the user of the
                    "Applicatio n" class (as in my example) or the "MultiCollectio n" class (as
                    in Matthew's example) can use each of the sub lists. You should do this
                    even if you don't need the extra validation now. You don't want to
                    change the interfaces of your components later if it is decided that
                    validation is needed. This is the same reason it is not recommended you
                    make public fields, but instead wrap a dummy property get and set around
                    it at the least.

                    I hope we've cleared this up for you?

                    Michael Lang, MCSD

                    Comment

                    • Matthew W. Jackson

                      #11
                      Re: Indexers

                      hehe.. rushed post

                      "Michael Lang" <ml@nospam.co m> wrote in message
                      news:Xns9446ACB CEA688lang1474i cqmailcom@207.4 6.248.16...[color=blue]
                      > "Matthew W. Jackson" <themuujAThotma ilDOTcom@NOSPAM .NOSPAM> wrote in
                      > news:Kstzb.9571 7$Vs1.64031@twi ster.austin.rr. com:
                      >[color=green]
                      > > You can do it, but it's more than just an indexer. Basically, you
                      > > have a method that returns an ICollection for each of your internal
                      > > Array lists. You can write your own private class that implements
                      > > ICollection and return it, in case you don't want to allow certain
                      > > operations, such as Insert, Remove, Set, etc. The point is, you can
                      > > control access to your internal array list, and even strongly type the
                      > > data.
                      > >
                      > > For a good example, look at how Hashtable lets you access Keys and
                      > > Values. There are two properties that both return ICollection that
                      > > basically give you two different views of the same data structure. As
                      > > a result, you can do hashtable.Keys[0] and hashtable.Items[0]. Even
                      > > better, you can use foreach() on these properties.[/color]
                      >
                      > Your general idea is correct. But, I'm guessing you meant to say:
                      >
                      > "Basically, you have a property that returns an ICollection for each of
                      > your internal Array lists." (swapped method out for property).
                      >
                      > The "Keys" and the "Values" members of hashtable are properties, not
                      > methods. If they were methods they would have parenthesis when you
                      > reference them, IE:
                      >
                      > hashtable.Keys( )[0] and hashtable.Value s()[0]
                      >
                      > Also note there is no "Items" property. The "Values" property holds the
                      > hashtable's values. The "Item" (no s), is the property that is also the
                      > indexer for the hashtable, which really just returns items from the
                      > "Values" property of the hashtable.
                      >
                      > Kudos on the excellent example. My "documents" example (same idea)
                      > propably should have exposed a second and third sub list property to be as
                      > clear as yours.
                      >
                      > Michael Lang, MCSD[/color]


                      Comment

                      Working...