Syntax for a static indexer (or it is not possible) ?

  • Thread starter Thread starter Michel Walsh
  • Start date Start date
M

Michel Walsh

Hi,


Looking for the syntax for a static indexer. For a non static 'access',
the following would do:

public class whatever
{
static Hashtable myHashtable = null;

// ----- here-----
public whatever this[string name]
{
get
{
return (whatever) myHashtable[name] ;
}
}

...
}


Clearly, that does not work prefixing with static,


static public whatever this[string name] {get{return (whatever)
myHashtable[name];} }


since 'this' cannot be used inside a static definition.





Vanderghast, Access MVP
 
Michel Walsh said:
Looking for the syntax for a static indexer.

Unfortunately there's no such thing in C#. The C# team decided it
wasn't a good idea, although they've said they might revisit that
decision some time.
 
Hi,
Static indexers are not available in C#.

Hi,


Looking for the syntax for a static indexer. For a non static 'access',
the following would do:

public class whatever
{
static Hashtable myHashtable = null;

// ----- here-----
public whatever this[string name]
{
get
{
return (whatever) myHashtable[name] ;
}
}

...
}


Clearly, that does not work prefixing with static,


static public whatever this[string name] {get{return (whatever)
myHashtable[name];} }


since 'this' cannot be used inside a static definition.





Vanderghast, Access MVP
 
Could you not have a helper class, and then declare a static variable of the
type of this helper class in your main class, the helper class could then
implement the indexer itself (there would only be one instance of it).
 
Hi,


I originally thought to make a static property, Item, and then use
whatever.Item( someName )

but the environment (it should be distribute to other developers) would
have been more "natural", more "cultural" using an indexer. I have to think
if there is not something along the lines you suggest, but my first
impression is that if I found back a "indexing" syntax, it may be with a
little extra complexity... and you know how developers are.... if they find
something is uselessly complex ... :-)


Vanderghast, Access MVP


Patty O'Dors said:
Could you not have a helper class, and then declare a static variable of
the
type of this helper class in your main class, the helper class could then
implement the indexer itself (there would only be one instance of it).


Michel Walsh said:
Hi,


Looking for the syntax for a static indexer. For a non static
'access',
the following would do:

public class whatever
{
static Hashtable myHashtable = null;

// ----- here-----
public whatever this[string name]
{
get
{
return (whatever) myHashtable[name] ;
}
}

...
}


Clearly, that does not work prefixing with static,


static public whatever this[string name] {get{return (whatever)
myHashtable[name];} }


since 'this' cannot be used inside a static definition.





Vanderghast, Access MVP
 
Back
Top