Link to home
Start Free TrialLog in
Avatar of IUAATech
IUAATech

asked on

help displaying stringdictionary contents

I am trying to display the contents of a stringdictionary.
The problem is that the order in which the the contents are being displayed is not the same as the order in which they are being inserted into the dictionary.

For example, if I do the following:
        StringDictionary data = new StringDictionary();
        data.Add("Submitted On:", Convert.ToString(DateTime.Now));
        data.Add("Request completion date:", Convert.ToString(calUserCompletionDate.SelectedDate.ToShortDateString()));
        data.Add("Job title:", txtJobTitle.Text);
        data.Add("Platform:", ddlPlatform.SelectedItem.Text);
        data.Add("Software:", ddlSoftware.SelectedItem.Text);
        data.Add("Hardware:", ddlHardware.SelectedItem.Text);
        data.Add("Notes:", txtNotes.Text);

the order of the items that are displayed when I do the following
        foreach (DictionaryEntry de in Data)
        {
            body += string.Format("{0}, {1}", de.Key, de.Value);
        }

is as follows:
software
platform
completion date
submitted on
job title
notes
hardware

so stringdictionary doesn't follow the FIFO rule? If not, how do I make this work? use some other data structure?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

"so stringdictionary doesn't follow the FIFO rule?"

No it does not.  A StringDictionary is just a HashTable that only works with Strings.

See HashTable:
http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.aspx

    "Represents a collection of key/value pairs that are organized based on the hash code of the key."

So the order of the items in the StringDictionary is based upon the the hash codes of the keys.

A HashTable is designed to allow you to quickly retrieve related items based on a KEY.  If you need FIFO capabilities then you want something like an ArrayList.  But then you don't get the quick "retrieval by key" ability.

You can't have it both ways...
Avatar of IUAATech
IUAATech

ASKER

can you have a 2D arraylist?
Not in the strict sense like you can have a 2D Array.

But you can put ANYTHING (instances of custom classes might be useful) into an ArrayList so I suppose technically you can have a "2D arraylist".
Here is what I want to accomplish:
I want something that supports "value1, value2" entry (LIFO) so that I can iterate over it and print out value1 and value2 next to value1.

What do you suggest I use?
If you want the best of both worlds then you need to use two structures.

Use a StringDictionary as you already are to store the key/value pair relationship.  

Then use the Stack class to hold just the Keys:
http://msdn2.microsoft.com/en-us/library/system.collections.stack.aspx

Iterate over the Stack to get each key in LIFO order and then look up the corresponding value in your StringDictionary.
this is getting to be complicated. How about if I don't need to do a lookup using keys?

So, all I want is to store <value1, value2> pair in a datastructure. Then I just iterate over this structure and print the values next to each other.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For LIFO, use a Stack.
For FIFO, use a Queue or ArrayList.  (or a List in C# 2005)
Now that I think about it, the values don't have to distinct. And I can't use the second approach since some of the inputs will be coming from a textbox control and might already contains a comma.
maybe I will just create a custom class then...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just what the doctor ordered. Thanks JimBrandley.
My pleasure.
one more question: what kind of performance issue am I looking at if I don't set the size of the list? Say I do something like: List<string[]> pairs = new List<string[]>();
That really depends on how many items you add to the List...

If you know beforehand the approximate size of the List, then pass that to the Constructor:
http://msdn2.microsoft.com/en-us/library/dw8e0z9z.aspx

    "If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List."
thanks.