indexers

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

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