You can do it, but it's more than just an indexer. Basically, you have a
method that returns an ICollection for each of your internal Array lists.
You can write your own private class that implements ICollection and return
it, in case you don't want to allow certain operations, such as Insert,
Remove, Set, etc. The point is, you can control access to your internal
array list, and even strongly type the data.
For a good example, look at how Hashtable lets you access Keys and Values.
There are two properties that both return ICollection that basically give
you two different views of the same data structure. As a result, you can do
hashtable.Keys[0] and hashtable.Items[0]. Even better, you can use
foreach() on these properties.
Here's a skeleton of how you'd do it:
public class MultiCollection {
private SubList list1;
private SubList list2;
private SubList list3;
// so on...
private class SubList : ICollection {
internal ArrayList list;
// implement all other required members, throwing exceptions on
unsupported operations
}
public ICollection Indexer1 {
get {
return list1;
}
}
public ICollection Indexer2 {
get {
return list2;
}
}
// so on...
}
Whether you decide to put the list logic in your private SubList class or in
the container class is up to you. It depends on what you're doing. In the
case of the Hashtable, the sub-lists (Keys and Values) basically hold a
pointer to the parent hashtable and access it's private data directly.
--Matthew W. Jackson
"Alvin Bruney" <alvin.bruney@.telia..com.> wrote in message
news:OS**************@TK2MSFTNGP09.phx.gbl...
yes but that means I have to refer to it as the class, I could have many
different arrays in a class and I want to use an indexer on more than one.
Methods are the only way it seems.
I was looking for somethin glike a property and an indexer for that kind
of scenario (yes i know properties are just methods anyway)
"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16... "Alvin Bruney" <alvin.bruney@.telia.com.> wrote in news:O8vc$dQtDHA.2408
@tk2msftngp13.phx.gbl:
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[i];
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