obtain member of Dictionary stored at lowest internal ordinal

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

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.
 
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
 
For,

KeyValuePair<string,MyClass> kvp =
(KeyValuePair<string,MyClass>)myClasses[0];


I get a compile error : invalid argument.


Peter Duniho said:
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
 
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
 
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 );

?


Peter Duniho said:
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
 
John said:
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();
 
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.
 
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.

Assuming this could happen, I can sum it up like this: "Life is like a
box of chocolates ... you never know what you're gonna get". As a
dictionary is ordered, at least from the user perspective, randomly,
first in does not mean ordinal 0.

If you need a true first in = ordinal 0, you can do it like this:

SortedList<int, MyClass> sl = new SortedList<int, MyClass>();

But, this does not allow easily retrieval by key, which is a string. It
is very efficient by ordinal, however.

You could load two objects:

SortedList<int, MyClass> sl = new SortedList<int, MyClass>();
Dictionary<string, MyClass> sl = new Dictionary<string, MyClass>();

And then make the choice of which object by ordinal or key. But you are
better to wrap something like this up:

public class MyDictionary : Dictionary<string, MyClass>
{
private SortedList<int, MyClass> sl =
new SortedList<int, MyClass>();

private int counter;

public MyDictionary()
{
counter = 0;
}

public override Add(string key, MyClass val)
{
base.Add(key, val);

sl.Add(counter, val);
}
}

Etc. I am not going to code the entire thing. And, I would still have to
think through the issue of how to accomplish something like the
following syntax:

MyClass class1 = MyDictionary[0];
MyClass class2 = MyDictionary["keyName"];

I am not sure it can be done, or at least not with a lot of very complex
code. The following is simple:

MyClass class1 = MyDictionary.GetOrdinal(ordinalVal);
MyClass class2 = MyDictionary["keyName"];

As is this if you derive from SortedList instead:

MyClass class1 = MyDictionary[0];
MyClass class2 = MyDictionary.GetFromKey("keyName");

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top