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

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
 
J

Jon Skeet [C# 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.
 
S

Shiva

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
 
G

Guest

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).
 
M

Michel Walsh

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top