ListDictionary.Item - where is it ?

  • Thread starter Thread starter gerry
  • Start date Start date
G

gerry

VS.NET 2003
All of the docs state that ListDictionary, ArrayList and others have a
public property Item used to access the collection contents using a numeric
index.
But the compiler is telling me different :
'System.Collections.Specialized.ListDictionary' does not contain a
definition for 'Item'
I have not been able to find any other references to this problem.
Are the docs completely baked on this or am I missing something basic here ?
Any Suggestions ?

Gerry
 
gerry said:
Are the docs completely baked on this or am I missing something basic here ?

You are missing something basic. The "Item" is a syntax for the indexer.
You are able to access the elements of a collection, for example, with
this syntax: collection[0]. You are effectively passing in an argument
(the 0 in this case) and a value is returned from that index in the
collection.

I remember I know at one point why it was always written "Item", but I
forgot.



Oliver Sturm
 
ok - this is not what the docs or associated examples clearly state, but i
will take your word for it and assume that the docs are just baked.

this does/can cause a problem though.

if you add a number of items as :
ld.Add("Item1","value1");
ld.Add("Item2","value2");
ld.Add("Item3","value3");

then you can access them as :
ld["Item1"];
ld["Item2"];
ld["Item3"];

but there is no way to access them as :
ld[0];
ld[1];
ld[2];
ie. i want the say the 1st or last item and I have no idea what any of the
contained item ids are.
these all return null which imo they should

this is what the docs state that the Item property was supposed to be for.

I suppose i could do :
foreach( DictionaryEntry de in ld ) {
val=de.Value;
exit;
}
or
((DictionaryEntry)ld.GetEnumerator().Current).Value;
but that seems rather ridiculous to me - mucho overhead.

there is also ld.Keys & ld.Values but I haven't figure out to actually get
anything out of these 2 properties other than to CopyTo an Array.




Oliver Sturm said:
gerry said:
Are the docs completely baked on this or am I missing something basic
here ?

You are missing something basic. The "Item" is a syntax for the indexer.
You are able to access the elements of a collection, for example, with
this syntax: collection[0]. You are effectively passing in an argument
(the 0 in this case) and a value is returned from that index in the
collection.

I remember I know at one point why it was always written "Item", but I
forgot.



Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
gerry said:
ok - this is not what the docs or associated examples clearly state, but i
will take your word for it and assume that the docs are just baked.

No, the docs state:

<quote>
In C#, this property is the indexer for the ListDictionary class.
</quote>

I can't see anything in the examples which suggests anything else.
this does/can cause a problem though.

if you add a number of items as :
ld.Add("Item1","value1");
ld.Add("Item2","value2");
ld.Add("Item3","value3");

then you can access them as :
ld["Item1"];
ld["Item2"];
ld["Item3"];
Indeed.

but there is no way to access them as :
ld[0];
ld[1];
ld[2];
ie. i want the say the 1st or last item and I have no idea what any of the
contained item ids are.

Then you should maintain a parallel IList implementation. A
ListDictionary is an IDictionary which happens to be implemented using
a list internally - it isn't a list in itself.
these all return null which imo they should
Absolutely.

this is what the docs state that the Item property was supposed to be for.

No it's not. The Item property
I suppose i could do :
foreach( DictionaryEntry de in ld ) {
val=de.Value;
exit;
}
or
((DictionaryEntry)ld.GetEnumerator().Current).Value;
but that seems rather ridiculous to me - mucho overhead.

More importantly, there's no guarantee that that will come out in the
order you expect.
 
gerry said:
if you add a number of items as :
ld.Add("Item1","value1");
ld.Add("Item2","value2");
ld.Add("Item3","value3");

then you can access them as :
ld["Item1"];
ld["Item2"];
ld["Item3"];

If the class in question has an indexer that takes a string argument
instead of the int that I used in my sample, you'll have to pass in a
string to get to the items. This is true, for example, for the Hashtable
or Dictionary said:
but there is no way to access them as :
ld[0];
ld[1];
ld[2];

If you want to access the items by a string key AND an int key, you'll
have to have multiple indexers that take different types of parameters.
How you implement that is not the business of the .NET docs :-) As John
suggests, you'll probably want to add an additional collection structure
to your storage class, to be able to access the items efficiently from
both indexers.


Oliver Sturm
 
Back
Top