Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

ListDictionary.Item - where is it ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gerry - 02 Aug 2005 19:20 GMT
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
Oliver Sturm - 02 Aug 2005 19:40 GMT
> 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
Signature

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 - 02 Aug 2005 20:26 GMT
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.

> > Are the docs completely baked on this or am I missing something basic here ?
>
[quoted text clipped - 8 lines]
>
>                 Oliver Sturm
Jon Skeet [C# MVP] - 02 Aug 2005 22:42 GMT
> 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.
>
[quoted text clipped - 7 lines]
>     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 ) {
[quoted text clipped - 4 lines]
>     ((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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Oliver Sturm - 03 Aug 2005 09:49 GMT
> if you add a number of items as :
>     ld.Add("Item1","value1");
[quoted text clipped - 5 lines]
>     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<T> and similar classes.

> 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
Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.