|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
obtain member of Dictionary stored at lowest internal ordinalDictionary<string,MyClass> myClasses; Is it possible to obtain a reference to object stored internally at the lowest ordinal in the Dictionary , without using the key ? This does not work : KeyValuePair<string,MyClass> kvp = (KeyValuePair<string,MyClass>)myClasses[0]; MyClass myClass0 = (MyClass)kvp.Value; Also, this does not work : KeyValuePair<string,MyClass> kvp = (KeyValuePair<string,MyClass>)myClasses.Values[0]; MyClass myClass0 = (MyClass)kvp.Value; If not possible to obtain reference to object stored at lowest ordinal in Dictionary , is it possible to obtain a reference to *any* object stored in the dictionary ? i.e. don't care what internal ordinal it's stored at. On Wed, 08 Jul 2009 17:37:31 -0700, John A Grandy
<johnagrandy-at-gmail-dot-com> wrote: > For : A Dictionary collection isn't ordered. There's no element that can be > > Dictionary<string,MyClass> myClasses; > > Is it possible to obtain a reference to object stored internally at the > lowest ordinal in the Dictionary , without using the key ? considered "the lowest ordinal". That said, at any given moment you can of course enumerate the elements, and the first one might be considered "the lowest ordinal" at that moment. But there's no guarantee it will remain the lowest ordinal after any kind of modification to the Dictionary. It's hard to tell from your examples what your actual requirement is, because you don't bother to state _why_ each example doesn't work. > [...] Of course. You can always retrieve an arbitrary element within the > If not possible to obtain reference to object stored at lowest ordinal in > Dictionary , is it possible to obtain a reference to *any* object stored > in > the dictionary ? i.e. don't care what internal ordinal it's stored at. Dictionary. You can even retrieve the "first" element in the Dictionary at any moment, as long as you don't care whether that element is always going to be the first element. The examples you gave a pretty close to what you might try if you don't care about the element always being present in the same place in the collection (the first seemed entirely fine, and the second would have been fine if you didn't have a type mismatch), so without a clear statement as to why those examples aren't appropriate for your needs, it's not possible to suggest a proper alternative. Pete For,
KeyValuePair<string,MyClass> kvp = (KeyValuePair<string,MyClass>)myClasses[0]; I get a compile error : invalid argument. Show quoteHide quote "Peter Duniho" <no.peted.spam@no.nwlink.spam.com> wrote in message news:op.uwrzvezuvmc1hu@macbook-pro.local... > On Wed, 08 Jul 2009 17:37:31 -0700, John A Grandy > <johnagrandy-at-gmail-dot-com> wrote: > >> For : >> >> Dictionary<string,MyClass> myClasses; >> >> Is it possible to obtain a reference to object stored internally at the >> lowest ordinal in the Dictionary , without using the key ? > > A Dictionary collection isn't ordered. There's no element that can be > considered "the lowest ordinal". > > That said, at any given moment you can of course enumerate the elements, > and the first one might be considered "the lowest ordinal" at that > moment. But there's no guarantee it will remain the lowest ordinal after > any kind of modification to the Dictionary. > > It's hard to tell from your examples what your actual requirement is, > because you don't bother to state _why_ each example doesn't work. > >> [...] >> If not possible to obtain reference to object stored at lowest ordinal in >> Dictionary , is it possible to obtain a reference to *any* object stored >> in >> the dictionary ? i.e. don't care what internal ordinal it's stored at. > > Of course. You can always retrieve an arbitrary element within the > Dictionary. You can even retrieve the "first" element in the Dictionary > at any moment, as long as you don't care whether that element is always > going to be the first element. > > The examples you gave a pretty close to what you might try if you don't > care about the element always being present in the same place in the > collection (the first seemed entirely fine, and the second would have been > fine if you didn't have a type mismatch), so without a clear statement as > to why those examples aren't appropriate for your needs, it's not possible > to suggest a proper alternative. > > Pete On Wed, 08 Jul 2009 18:19:49 -0700, John A Grandy
<johnagrandy-at-gmail-dot-com> wrote: > For, How is "myClasses" declared? If that's your Dictionary itself, no...it > > KeyValuePair<string,MyClass> kvp = > (KeyValuePair<string,MyClass>)myClasses[0]; > > I get a compile error : invalid argument. wouldn't. You can only index by the key itself. It would work if it was the result of converting your Dictionary via IEnumerable and then to, for example, a List<KeyValuePair<string, MyClass>>. Of course, a better approach might be just to do this: KeyValuePair<string, MyClass> kvp = myClasses.First(); Or even, depending on what data you actually want to retrieve: MyClass element = myClasses.Values[0]; But again, your question isn't very specific. If you need the element returned to always be the same as the collection changes, that's not guaranteed to work. If you just need any arbitrary element and the first one just happens to be handy, then either of the above ought to work. Pete Right, I just need any arbitrary element.
Something like this ... List<KeyValuePair<string, MyClass>> listMyClasses = new List<KeyValuePair<string, MyClass>>( (IEnumerable<KeyValuePair<string, MyClass>>)myClasses ); ? Show quoteHide quote "Peter Duniho" <no.peted.spam@no.nwlink.spam.com> wrote in message news:op.uwr1gotovmc1hu@macbook-pro.local... > On Wed, 08 Jul 2009 18:19:49 -0700, John A Grandy > <johnagrandy-at-gmail-dot-com> wrote: > >> For, >> >> KeyValuePair<string,MyClass> kvp = >> (KeyValuePair<string,MyClass>)myClasses[0]; >> >> I get a compile error : invalid argument. > > How is "myClasses" declared? If that's your Dictionary itself, no...it > wouldn't. You can only index by the key itself. It would work if it was > the result of converting your Dictionary via IEnumerable and then to, for > example, a List<KeyValuePair<string, MyClass>>. > > Of course, a better approach might be just to do this: > > KeyValuePair<string, MyClass> kvp = myClasses.First(); > > Or even, depending on what data you actually want to retrieve: > > MyClass element = myClasses.Values[0]; > > But again, your question isn't very specific. If you need the element > returned to always be the same as the collection changes, that's not > guaranteed to work. If you just need any arbitrary element and the first > one just happens to be handy, then either of the above ought to work. > > Pete On Wed, 08 Jul 2009 18:52:04 -0700, John A Grandy
<johnagrandy-at-gmail-dot-com> wrote: > Right, I just need any arbitrary element. Then either of the examples I offered should work. If they don't, you need to explain why so we can understand the question better. John A Grandy wrote:
Show quoteHide quote > For : Is something like this what you are after?> > Dictionary<string,MyClass> myClasses; > > Is it possible to obtain a reference to object stored internally at the > lowest ordinal in the Dictionary , without using the key ? > > This does not work : > > KeyValuePair<string,MyClass> kvp = > (KeyValuePair<string,MyClass>)myClasses[0]; > MyClass myClass0 = (MyClass)kvp.Value; > > Also, this does not work : > > KeyValuePair<string,MyClass> kvp = > (KeyValuePair<string,MyClass>)myClasses.Values[0]; > MyClass myClass0 = (MyClass)kvp.Value; > > > If not possible to obtain reference to object stored at lowest ordinal in > Dictionary , is it possible to obtain a reference to *any* object stored in > the dictionary ? i.e. don't care what internal ordinal it's stored at. > > Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("Indiana", "Indianapolis"); dict.Add("Utah", "Salt Lake City"); dict.Add("Bavaria", "Munich"); Console.WriteLine(string.Format("First Key: {0}", dict.Keys.First())); Console.WriteLine( string.Format("First Entry: {0}", dict[dict.Keys.First()])); Console.ReadLine(); -- Mike
Other interesting topics
ContextMenu Does Not Work?
Handlling different Data types Replace strings in a text file and get the number of replacements made linq to sql speed question Difficulty with Regex pattern to validate a string Input - String : Output - Binary Oct Dec Hex Need advice on Class. Thank You Drag Drop - Move Data Between Apps How to use bring to front and Send to back so the rsult is good Show application at Windows Vista Login/Locked screen |
|||||||||||||||||||||||