• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

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?
0
IUAATech
Asked:
IUAATech
  • 8
  • 6
  • 2
  • +1
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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...
0
 
IUAATechAuthor Commented:
can you have a 2D arraylist?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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".
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
IUAATechAuthor Commented:
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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
sabeeshCommented:
Why cant you use a stack or queue for this

http://msdn2.microsoft.com/en-us/library/aa289149(VS.71).aspx
0
 
IUAATechAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want the values to be DISTINCT from each other, then write a custom class to hold those two values and places instances of that class into a Stack.

Or you could just cram the two values together into ONE string, seperated by a comma for instance, and place that into the Stack.  Then if you needed to seperate them back out you could use String.Split().
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For LIFO, use a Stack.
For FIFO, use a Queue or ArrayList.  (or a List in C# 2005)
0
 
IUAATechAuthor Commented:
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.
0
 
IUAATechAuthor Commented:
maybe I will just create a custom class then...
0
 
JimBrandleyCommented:
IUAATech - If you are using .Net 2.0, you can use this:
List<string[]> pairs = new List<string[]>(20);

pairs.Add( new string[] {"Submitted on: ", Convert.ToString(DateTime.Now)});
// etc

// Then to get them.
foreach (string[] pair in pairs)
{
      body += string.Format("{0}, {1}", pair[0], pair[1]);
}

Note - When you declare the list class, set the capacity to your largest anticipated need so it doesn't need to re-allocate memory.

Jim
0
 
IUAATechAuthor Commented:
just what the doctor ordered. Thanks JimBrandley.
0
 
JimBrandleyCommented:
My pleasure.
0
 
IUAATechAuthor Commented:
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[]>();
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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."
0
 
IUAATechAuthor Commented:
thanks.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now