In later versions of the framework is it possible to have indexers on
properties for member fields?
Say for example I have
private ArrayList blah;
and a propget
public object Blah[int index]
{
get { return blah[index]; }
}
There is only one thing wrong with the indexer in your code snippet. You
forgot the "this" keyword. Here is a snippet from one of my custom
collection classes...
public class EntityCollection:CollectionBase
{
// "this.List" is an ArrayList that is a member of CollectionBase
public Entity this[int index]
{
get { return (Entity)this.List[index]; }
}
//...<Snip>
}
To access a member of this collection...
EntityCollection ec = new EntityCollection();
ec.Add(...);
Entity e1 = ec[0];
If you want to access a single element from a member collection, then you
should be calling the indexer on that member property... IE.
//A collection of the "Document" class
public class DocumentCollection:CollectionBase{...}
public class Application
{
private DocumentCollection _docs;
public Application(){_docs=new DocumentCollection();}
public DocumentCollection Documents{get{return _docs;}}
}
to access a document of the application...
Application app = new Application();
... add some documents here...
Document doc = app.Documents
;
Let me know if you have further questions.
To see how to create a collection like the ones above, use my open source
collection generator/templates found at:
http://sourceforge.net/projects/colcodegen
Michael Lang, MCSD