indexers

  • Thread starter Thread starter phoenix
  • Start date Start date
P

phoenix

Hello,

I have the following class :

public class Unit {
Hashtable a;
Hashtable b;

// constructors

public object this[string name]
{
get
{
return a[name];
}
}
}

Which works like a charm, but now i want a second property say size on which
i can use an indexer as well. So something like this

public object size[string name]
{
get
{
return b[name];
}
}

but i don't find a way to get it working. Any ideas ?

TIA

Yves
 
phoenix,

In this case, you have to expose the Hashtable itself, so you would do
this:

public Hashtable size
{
get
{
return b;
}
}

Of course, that leaves your hashtable open to modification, so you might
not want to do that. In that case, you will want to create your own read
only dictionary that does not allow the setting of the values outside of
your code.

Hope this helps.
 
Back
Top