Why can't I make a static indexer?

  • Thread starter Thread starter Sam Martin
  • Start date Start date
S

Sam Martin

Basically, i've got a class called RegularExpressions 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.RegularExpressions[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
 
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 RegularExpressions 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.RegularExpressions.Expressions[RegEx.PostCode]

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

http://www.dotnetconsult.co.uk/weblog/PermaLink.aspx/69ec7c34-ae29-4778-952b-1b6539303e6b

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog/
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Basically, i've got a class called RegularExpressions 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.RegularExpressions[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
 
Back
Top