I have the following:
Dictionary<object, stringd = new Dictionary<object, string>();
I can add entries to the dictionary, and I can loop through the entries I've
added to the Dictionary. This all works great.
My question is: I'd like to find a way to get just the first key, without
looping through the whole Dictionary. I tried the code below, but I get a
compile error trying to use an index value [0] on the KeyCollection:
Dictionary<object, string>.KeyCollection keyColl = d.Keys;
object firstStyle = keyColl[0];
Any suggestions?
Thanks,
--
Randy 5 21536
OK - why you say "first", do you mean in insertion sequence? I'm not sure
this is retained in a dictionary...
Anyways, since the key-collection it is enumerable:
foreach(object key in keyCol) {
return key; or assign to a variable and then "break;"
}
// if here, then no key to get... oops!
Marc
Actually, it occurs that null keys are not allowed, so the following would
be quite tidy:
object firstKey = null;
foreach(object key in d.Keys) {
firstKey = key;
break;
}
// now if firstKey == null, no keys, else it is the first
Marc
Many thanks for the response. That's what I need.
You do raise an interesting question about whether the insertion sequence is
preserved. So far in my testing, it seems to be. Still, if it's not
guarenteed, I need to switch to a List<MyContainerClass>.
Does anybody know for sure if a Dictionary preserve the insertion sequence?
Thanks,
--
Randy
"Marc Gravell" wrote:
OK - why you say "first", do you mean in insertion sequence? I'm not sure
this is retained in a dictionary...
Anyways, since the key-collection it is enumerable:
foreach(object key in keyCol) {
return key; or assign to a variable and then "break;"
}
// if here, then no key to get... oops!
Marc
To quote from MSDN2 "The order of the keys in the Dictionary.KeyCollectionn
is unspecified"...
Also - if you always want the first one, you might want a Queue<T>
Marc
Actually - worth another note at this juncture re Queue<Tvs List<T>; now,
I don't know for sure with List<T>, but ArrayList (its older brother) was
known for *not* reclaiming empty space from removed elements - so if you
keep adding and removing elements then actually the underlying array keeps
growing (via doubling) inline with the total number of elements added. As I
understand it, Queue<Tdoes not suffer this, as the fact that we are always
removing items from the start makes it easy to reclaim space. But I honestly
don't know how List<Tbehaves here ;-p
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jason |
last post by:
From another post I was given a solution to a problem I was having
with creating a composite view of similiar rows.
...
|
by: Thanos Tsouanas |
last post by:
Hello.
I would like to have a quick way to create dicts from object, so that a
call to foo would return obj.bar.
The following works, but I would prefer to use a built-in way if one
exists. ...
|
by: Bob Whisnant |
last post by:
In Access 2000, I'm using the DoCmd.TransferText to import a tab
delimited .txt file into my database. I'm using a specification file
to set field names and data type. The HasFieldNames argument is...
|
by: Dixie |
last post by:
How do I parse out the 20 from the current year (2005). I can get the last
2 numbers on their own, but have been unable to get the first two on their
own.
TIA
dixie
|
by: laredotornado |
last post by:
Hi, Using PHP 4, if I have a date, what is a function I could use to
give me a date that represents the first day of that month? For
example, if my date were "3/19/2006 8:00", I would want my...
|
by: CSharper |
last post by:
I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string array = {"help","Csharp rocks"}
...
|
by: reginaldmerritt |
last post by:
Hello
I'm have a form displaying records from a table holding details of events.
I'm using DFirst and DLast to work out what is the First and Last date.
I have the following code which...
|
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...
|
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...
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |