[Mono-list] properties with indexers.
Stuart Ballard
stuart.ballard@corp.fast.net
Fri, 25 Jul 2003 10:34:20 -0400
Yves Kurz wrote:
> Hallo..
>
> maybe anyone of you can help me with this. Is it possible to write
> properties which uses indexers?
>
> I tried someting like this but i can't compile it:
>
> class A {
> protected object [] o;
>
> public object O [int i] {
> get { return o[i]; }
> set { o[i] = value; }
> }
> }
You want something like this:
class A {
protected internal object[] o;
private AO ao = new AO(this);
public AO O {
get {return ao;}
}
}
class AO {
private A a;
internal AO(A a) {this.a = a;}
public object this[int i] {
get {return a.o[i];}
set {a.o[i] = value;}
}
}
Basically the trick is to have a readonly property which returns an
object that supports the indexer. All the other code in the AO class is
just keeping track of which particular A's O you want to return. Note
that I made several of the properties internal - this may be necessary
so that A and AO can talk to each other - as far as I can tell, C#
supports nested classes like Java, but *doesn't* give those classes
access to private members.
Stuart.
--
Stuart Ballard, Senior Web Developer
FASTNET - Web Solutions
(215) 283-2300, ext. 126
www.fast.net