Why can't I make a static indexer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam Martin

    Why can't I make a static indexer?

    Basically, i've got a class called RegularExpressi ons which contains a
    private Hashtable of RegExps used in my apps.

    I obviously don't want to give full public access to the Hashtable, so I'd
    like to define a static indexer so that the following syntax is possible

    e.g.
    ns.RegularExpre ssions[RegEx.PostCode]

    I've got a static method that does this, but i want to know if it's possible
    to create a static indexer?

    tia
    sam martin


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Why can't I make a static indexer?

    Sam,

    No, it is not (if it was, they might have chosen a better word to
    represent it).

    I believe that the intent behind an indexer is to represent ways of
    accessing items in collections (think of collections as a generic term
    here). Your RegularExpressi ons class doesn't appear to be strictly a
    collection (then again, that's hard to say). Rather, create a strongly
    typed collection which returns your expressions, and then expose that
    through a static property. Your syntax would be:

    ns.RegularExpre ssions.Expressi ons[RegEx.PostCode]

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Sam Martin" <sambomartin@ya hoo.co.uk> wrote in message
    news:uFLY3mzxEH A.1564@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Basically, i've got a class called RegularExpressi ons which contains a
    > private Hashtable of RegExps used in my apps.
    >
    > I obviously don't want to give full public access to the Hashtable, so I'd
    > like to define a static indexer so that the following syntax is possible
    >
    > e.g.
    > ns.RegularExpre ssions[RegEx.PostCode]
    >
    > I've got a static method that does this, but i want to know if it's
    > possible to create a static indexer?
    >
    > tia
    > sam martin
    >[/color]


    Comment

    • Richard Blewett [DevelopMentor]

      #3
      Re: Why can't I make a static indexer?

      I wrote a blog entry a while back precisely about this issue. You can read it here:



      Regards

      Richard Blewett - DevelopMentor



      nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<uFLY3mzxEHA.15 64@TK2MSFTNGP09 .phx.gbl>

      Basically, i've got a class called RegularExpressi ons which contains a
      private Hashtable of RegExps used in my apps.

      I obviously don't want to give full public access to the Hashtable, so I'd
      like to define a static indexer so that the following syntax is possible

      e.g.
      ns.RegularExpre ssions[RegEx.PostCode]

      I've got a static method that does this, but i want to know if it's possible
      to create a static indexer?

      tia
      sam martin

      Comment

      • Sam Martin

        #4
        Re: Why can't I make a static indexer?

        thanks

        "Sam Martin" <sambomartin@ya hoo.co.uk> wrote in message
        news:uFLY3mzxEH A.1564@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Basically, i've got a class called RegularExpressi ons which contains a
        > private Hashtable of RegExps used in my apps.
        >
        > I obviously don't want to give full public access to the Hashtable, so I'd
        > like to define a static indexer so that the following syntax is possible
        >
        > e.g.
        > ns.RegularExpre ssions[RegEx.PostCode]
        >
        > I've got a static method that does this, but i want to know if it's
        > possible to create a static indexer?
        >
        > tia
        > sam martin
        >[/color]


        Comment

        Working...