Home All Groups Group Topic Archive Search About

obtain member of Dictionary stored at lowest internal ordinal

Author
9 Jul 2009 12:37 AM
John A Grandy
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 ?

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.

Author
9 Jul 2009 1:03 AM
Peter Duniho
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
Are all your drivers up to date? click for free checkup

Author
9 Jul 2009 1:19 AM
John A Grandy
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
Author
9 Jul 2009 1:37 AM
Peter Duniho
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
Author
9 Jul 2009 1:52 AM
John A Grandy
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
Author
9 Jul 2009 4:46 AM
Peter Duniho
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.
Author
9 Jul 2009 2:30 AM
Family Tree Mike
John A Grandy wrote:
Show quoteHide quote
> 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 ?
>
> 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.
>
>

Is something like this what you are after?

   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

Bookmark and Share