473,773 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 62840
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 OrderedDictiona ry class in the System.Collecti ons.Specialized namespace
(which is not generic) or the KeyedCollection class in the
System.Collecti ons.ObjectModel namespace (which is generic).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Frank Rizzo" <no**@none.comw rote in message
news:e4******** ******@TK2MSFTN GP05.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.comw rote:
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.Valu es[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
SortedDictionar y<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.comw rote:
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.co m>
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.c omwrote:
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 OrderedDictiona ry class in the System.Collecti ons.Specialized
namespace
(which is not generic)
Can you confirm that the order of elements in an OrderedDictiona ry are in
fact maintained in the order in which they were added?

I couldn't find anything in the documentation for OrderedDictiona ry that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictiona ry 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.co m

"Peter Duniho" <Np*********@nn owslpianmk.comw rote 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.c omwrote:
> 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 OrderedDictiona ry class in the System.Collecti ons.Specialized
namespace
(which is not generic)

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

I couldn't find anything in the documentation for OrderedDictiona ry that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictiona ry 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.co m

"Peter Duniho" <Np*********@nn owslpianmk.comw rote 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.c omwrote:
> 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 OrderedDictiona ry class in the System.Collecti ons.Specialized
namespace
(which is not generic)

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

I couldn't find anything in the documentation for OrderedDictiona ry that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictiona ry 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*********@nn owslpianmk.comw rote 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.c omwrote:
> 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 OrderedDictiona ry class in the System.Collecti ons.Specialized
namespace
(which is not generic)

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

I couldn't find anything in the documentation for OrderedDictiona ry that
supports *or* refutes that. The closest I came was this statement: "The
elements of an OrderedDictiona ry 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
OrderedDictiona ry 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.nos pamwrote:
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
OrderedDictiona ry 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

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

Similar topics

1
2920
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 works and the event fires except when the user selects the *first* item in the list, which appears to be the default selection, even though the generated HTML code does not have a "selected" property set. Is it possible to cause the...
3
4202
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 dropdownlist to a string var it always returns the first item regardless of what was selected code is simple
2
2855
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, "tblOrders").AddNew() Me.ComboBox1.SelectedIndex = -1 but... how do we deal with the same issue during record navigation? When I move forward or backward onto a null field the combo defaults to the first item on the list again?
5
9905
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 saying that I clicked the first item. Why can't I just select the first item to get an event to fire without having to first select any other item. Thanks for any tips.
0
1349
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 been select by default. So when I click the first items' first letter, it goes to next item starts with same letter. For example, I have
5
3264
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 3D the first item in the ddl to get it to fire for 3D. I have autopostback set to true. I have heard that I need to put a blank item in the ddl, but not sure how I would do this with a dataset.
1
2065
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 put a statement that orders the ListView by a certain column and in a certain order (ascending or descending, it doesn't matter). To do that, I have defined a derived class of the IComparer class (as explained in the Visual Studio MSDN Library)....
0
1004
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 combobox that is not the first item, that selected item replaces the first entry in the combobox and I cannot figure out how to get that first item back. I am using VB 2005 Express Any help would be greatly appreciated.
5
10702
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: for x in container.iterChildren(): How to skip the first item? It seems that it's a simple question. Could somebody help me? Thanks.
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.