473,322 Members | 1,501 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,322 software developers and data experts.

How to get the first item in Dictionary?

How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?

Regards
Jun 4 '07 #1
10 62785
Frank,

No, it is not, as the dictionary doesn't maintain an order in which the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized namespace
(which is not generic) or the KeyedCollection class in the
System.Collections.ObjectModel namespace (which is generic).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:e4**************@TK2MSFTNGP05.phx.gbl...
How do I get the first item that was added to the Dictionary object. I've
tried myDictionary[0], but it's not supported?

Regards

Jun 4 '07 #2
Frank Rizzo wrote:
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
That's not possible using the generic implementation as the underlying
storage is a hashtable and the order in which items are returned is
undefined. You get an item based on key, not based on the order that it
was added.
--
Tom Porterfield
Jun 4 '07 #3
On Mon, 04 Jun 2007 09:07:56 -0700, Frank Rizzo <no**@none.comwrote:
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
I'm not sure you can. Since Dictionary is a hashtable, items are not
stored in order of addition to the Dictionary.

You could try using foreach() and just grabbing the first item that is
returned, or using Dictionary.Values[0]. But I suspect both of those will
just return the item with the lowest-valued hash code. It probably won't
necessarily be the first one you added (and in fact, probably won't be
most of the time).

If you need to preserve order in your collection, I'd recommend using the
SortedDictionary<generic class instead, where the key you use is some
monotonically increasing value (DateTime.Now, or just a simple integer
counter, for example). In that case, the collection *does* have order,
and it makes sense to ask it for the "first" element.

Pete
Jun 4 '07 #4
Frank Rizzo <no**@none.comwrote:
How do I get the first item that was added to the Dictionary object.
I've tried myDictionary[0], but it's not supported?
As others have pointed out, dictionaries are inherently unsorted.
Basically, you need to keep a List as well.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 4 '07 #5
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
No, it is not, as the dictionary doesn't maintain an order in which
the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized
namespace
(which is not generic)
Can you confirm that the order of elements in an OrderedDictionary are in
fact maintained in the order in which they were added?

I couldn't find anything in the documentation for OrderedDictionary that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictionary are not sorted in any way". But that's
so vague that it doesn't, at least to me, actually say that you can't rely
on the order of the elements.

In other words, I see two possibilities:

* All that the "Ordered" means is that you can use an index to
retrieve elements from the collection, and that which element corresponds
to which index is undefined.

* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete
Jun 4 '07 #6
Peter,

While yes, the elements are not sorted by any attributes of the keys or
values, they are sorted by position, which is implied by the index. If the
numeric index didn't indicate position, then what would it represent?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> No, it is not, as the dictionary doesn't maintain an order in which
the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized
namespace
(which is not generic)

Can you confirm that the order of elements in an OrderedDictionary are in
fact maintained in the order in which they were added?

I couldn't find anything in the documentation for OrderedDictionary that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictionary are not sorted in any way". But that's
so vague that it doesn't, at least to me, actually say that you can't rely
on the order of the elements.

In other words, I see two possibilities:

* All that the "Ordered" means is that you can use an index to
retrieve elements from the collection, and that which element corresponds
to which index is undefined.

* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete

Jun 4 '07 #7
Also, the RemoveAt and Insert methods show that the numeric index is
infact a positional index. Granted, the order can be changed, but for any
other collection I have seen, adding a new item always adds it to the end of
the list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> No, it is not, as the dictionary doesn't maintain an order in which
the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized
namespace
(which is not generic)

Can you confirm that the order of elements in an OrderedDictionary are in
fact maintained in the order in which they were added?

I couldn't find anything in the documentation for OrderedDictionary that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictionary are not sorted in any way". But that's
so vague that it doesn't, at least to me, actually say that you can't rely
on the order of the elements.

In other words, I see two possibilities:

* All that the "Ordered" means is that you can use an index to
retrieve elements from the collection, and that which element corresponds
to which index is undefined.

* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete

Jun 4 '07 #8

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 09:31:37 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> No, it is not, as the dictionary doesn't maintain an order in which
the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized
namespace
(which is not generic)

Can you confirm that the order of elements in an OrderedDictionary are in
fact maintained in the order in which they were added?

I couldn't find anything in the documentation for OrderedDictionary that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictionary are not sorted in any way". But that's
so vague that it doesn't, at least to me, actually say that you can't rely
on the order of the elements.

In other words, I see two possibilities:

* All that the "Ordered" means is that you can use an index to
retrieve elements from the collection, and that which element corresponds
to which index is undefined.

The ordering appears to be well defined. If you use only the Add method, it
will be the order in which they were added, because

Add "Adds an entry with the specified key and value into the
OrderedDictionary collection with the lowest available index"
and
RemoveAt "The entries that follow the removed entry move up to occupy the
vacated spot and the indexes of the entries that move are also updated."

You can control the order more precisely using Insert.
* "Ordered" actually means that there *is* a defined order to the
collection and that this order, while not sorted, does correspond exactly
to the order in which items were added.

I haven't found anything in the documentation that would help resolve the
ambiguity. Do you have information that does? A link to a documentation
page would be especially helpful.

Thanks,
Pete

Jun 4 '07 #9
On Mon, 04 Jun 2007 13:17:13 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
The ordering appears to be well defined. If you use only the Add
method, it
will be the order in which they were added, because

Add "Adds an entry with the specified key and value into the
OrderedDictionary collection with the lowest available index" and
RemoveAt "The entries that follow the removed entry move up to occupy the
vacated spot and the indexes of the entries that move are also updated."
Thank you...that was the sort of thing I was looking for in the docs, but
didn't manage to find the right page. Much obliged.
Jun 4 '07 #10
On Mon, 04 Jun 2007 13:18:04 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
While yes, the elements are not sorted by any attributes of the keys
or
values, they are sorted by position, which is implied by the index. If
the
numeric index didn't indicate position, then what would it represent?
Well, I thought my post was clear enough on that. However, as an example
of the ambiguity, it could be that accessing an item via an index simply
returns items in the same order that foreach() does. All collections that
I've seen implement IEnumerable and so allow foreach(), but not all
collections mandate a specific order for items to be retrieved via
foreach().

In any case, while I am surprised that the reference for the Item property
doesn't make this clear, I agree that the reference for Add() and
RemoveAt() do. Thanks to you both for your replies.

Pete
Jun 4 '07 #11

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

Similar topics

1
by: Timo | last post by:
I am trying to use the DropDownList_SelectedIndexChanged event on a dropdown which is dynamically populated with different values at runtime, depending on what the user has been doing. The dropdown...
3
by: Siamak Zahedi | last post by:
Hi, weird problem I'm having cant seem to figure out what is going on. I have a dropdownlist that gets populated from db and a button that causes a post back when I assign the value of the...
2
by: Nate | last post by:
I have used the feedback on this issue to remedy my comboboxes showing the first item on the list when a new record is added to the binding context --- Me.BindingContext(dsOrders,...
5
by: glenn | last post by:
Hi folks, If I want to select the first item in a DropDownList, I need to first select any item other than the first item and then next I select the first item which will then fire an event...
0
by: Ryan Liu | last post by:
In a listview, I found it is pretty nice I can type a letter or more letter quickly to select the NEXT item which begin with that letter(s). But seems the first row, even not high lighted, always...
5
by: Cindy H | last post by:
Hi I have a ddl that I have populated with a dataset. I have 2 items in the ddl. The first one is '3D' and the second one is 'Spot'. I have to select Spot - the second item in the ddl and then...
1
by: polocar | last post by:
Hi, I'm using Visual C# 2005 (part of Visual Studio 2005 Professional Edition), and I have the following problem: I populate a ListView object with several items, and at the end of this process I...
0
by: uspazn | last post by:
Hello all, I am a newbie and this is my problem. I have a program that uses a simple access database. Seperate tables in the database fill several comboboxes. When the user selects an item in the...
5
by: ray | last post by:
A container object provides a method that returns an iterator object. I need to iterate the sequence with that iterator, but need to skip the first item. I can only iterate the whole sequence with:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.