473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Indexers

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]; }
}

Nov 15 '05 #1
10 1172
Alvin,
In later versions of the framework is it possible to have indexers on
properties for member fields?


No. But this is a C# language limitation.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Will it ever be added or is there a good reason for not having this?

Since propertys are methods anyway, I dont see a technicaly reason why they
cannot be permited

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
Alvin,
In later versions of the framework is it possible to have indexers on
properties for member fields?


No. But this is a C# language limitation.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #3
Alvin,
Will it ever be added or is there a good reason for not having this?
"Ever" is a long time, and since nothing beyond the Whidbey release
has been announced, I don't thing anyone can tell. It probably also
depends on customer demand.

Since propertys are methods anyway, I dont see a technicaly reason why they
cannot be permited


While they technically are implemented that way, I think conceptually
properties and methods are very different.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4
"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
Nov 15 '05 #5
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

Nov 15 '05 #6
"Alvin Bruney" <alvin.bruney@.telia..com.> wrote in
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...

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];


You can always make the ArrayList public instead of private, or create a public property
that returns that arrayList or a clone of it. If you need more control, or don't want
to waste resources making clones, then I still recommend my previous Documents sample.
Here is a example of making the members public:

================================================== ====================
using System;
using System.Collections;

namespace IndexerTest2
{
public class MyClass
{
public ArrayList Array1; //publically editable
private ArrayList _array2;

public MyClass()
{
Array1 = new ArrayList();
_array2 = new ArrayList();
_array2.Add("internal add test");
}
public ArrayList Array2
{ //if you don't want it to be editable return a clone.
get{return (ArrayList)_array2.Clone();}
}

[STAThread]
static void Main(string[] args)
{ //Testing code...
MyClass c1 = new MyClass();
c1.Array1.Add("test 1");
Console.WriteLine(c1.Array1[0].ToString());
c1.Array2.Add("test 2"); //does not add to _array2...
Console.WriteLine(c1.Array2[0].ToString());
Console.WriteLine(c1.Array2[1].ToString()); //this line fails
Console.ReadLine();
}
}
}
================================================== ====================
Nov 15 '05 #7
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


Nov 15 '05 #8
"Matthew W. Jackson" <th********************@NOSPAM.NOSPAM> wrote in
news:Ks*******************@twister.austin.rr.com:
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.


Your general idea is correct. But, I'm guessing you meant to say:

"Basically, you have a property that returns an ICollection for each of
your internal Array lists." (swapped method out for property).

The "Keys" and the "Values" members of hashtable are properties, not
methods. If they were methods they would have parenthesis when you
reference them, IE:

hashtable.Keys()[0] and hashtable.Values()[0]

Also note there is no "Items" property. The "Values" property holds the
hashtable's values. The "Item" (no s), is the property that is also the
indexer for the hashtable, which really just returns items from the
"Values" property of the hashtable.

Kudos on the excellent example. My "documents" example (same idea)
propably should have exposed a second and third sub list property to be as
clear as yours.

Michael Lang, MCSD
Nov 15 '05 #9
Michael Lang <ml@nospam.com> wrote in
news:Xn********************************@207.46.248 .16:
"Alvin Bruney" <alvin.bruney@.telia..com.> wrote in
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.
<snip>

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...

<snip>

//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 be a little more clear...

public DocumentCollection Documents{get{return _docs;}}

In the line above "Documents" is the property name.
"DocumentCollection" is the type of the property. I could have many
properties that all return a "DocumentCollection" with different names...

public DocumentCollection Documents{get{return _docs;}}
public DocumentCollection Papers{get{return _docs;}}
public DocumentCollection Reports{get{return _docs;}}

Your return type could just as easily by "ArrayList" instead of
"DocumentCollection". As Matthew also noted, you may want to create a
custom collection type instead of returning the raw array. This way you
can add extra validation to limit the types of operations the user of the
"Application" class (as in my example) or the "MultiCollection" class (as
in Matthew's example) can use each of the sub lists. You should do this
even if you don't need the extra validation now. You don't want to
change the interfaces of your components later if it is decided that
validation is needed. This is the same reason it is not recommended you
make public fields, but instead wrap a dummy property get and set around
it at the least.

I hope we've cleared this up for you?

Michael Lang, MCSD
Nov 15 '05 #10
hehe.. rushed post

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...
"Matthew W. Jackson" <th********************@NOSPAM.NOSPAM> wrote in
news:Ks*******************@twister.austin.rr.com:
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.


Your general idea is correct. But, I'm guessing you meant to say:

"Basically, you have a property that returns an ICollection for each of
your internal Array lists." (swapped method out for property).

The "Keys" and the "Values" members of hashtable are properties, not
methods. If they were methods they would have parenthesis when you
reference them, IE:

hashtable.Keys()[0] and hashtable.Values()[0]

Also note there is no "Items" property. The "Values" property holds the
hashtable's values. The "Item" (no s), is the property that is also the
indexer for the hashtable, which really just returns items from the
"Values" property of the hashtable.

Kudos on the excellent example. My "documents" example (same idea)
propably should have exposed a second and third sub list property to be as
clear as yours.

Michael Lang, MCSD

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
2
by: Jim | last post by:
How does one determine what indexers are available on a given object? The only way I have found is by looking at the Object Browser. But even then it only gives a simple signature like, this....
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
5
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
2
by: Raja Raman Sundararajan | last post by:
Hello guys, I was investigating how one can use the "text indexers" in python and I stumbled across several ones. eg., pylucene I wanted to know how the algorithm of indexers look like. I have...
3
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
3
by: daz_oldham | last post by:
Hi everyone I am just trying to find how to implement Indexers... or at least that is what I think I want to implement! Very basically, in my database I have a table of T_Hotels which has...
6
by: Beorne | last post by:
I have to use XmlSerializer to serialize a class and I've found big problems serializing properties and indexers. I assumed that if you serialize a class with public properties (or an indexers)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.