Hi, i wants to add key and value of enumerator into a dictionary list, what should i do? My code as below:

public void Display1()
         {                     
             Dictionary<string, double> sortList2 = new Dictionary<string, double>();                   
             Dictionary<string, double> sortList3 = new Dictionary<string, double>();   
             foreach (string sortComb in listbox1.Items)
             {
                 if (!(sortList2.ContainsKey(sortComb)))
                 {
                     sortList2.Add(sortComb, 1);
                 }
                 else
                 {
                     int Count = (int)sortList2[sortComb];
                     sortList2[sortComb] = Count + 1;
                 }
             }
             IDictionaryEnumerator enumerator2 = sortList2.GetEnumerator();

             string str1 = String.Empty;
             while (enumerator2.MoveNext())
             {             
                 listbox.Items.Add("(" + enumerator2.Key.ToString() + "): " + " " + enumerator2.Value.ToString() + "\n");               
                 /*str1 = enumerator2.Key.ToString();
                 double dblVar = (double)enumerator2.GetType().GetField("value__").GetValue(enumerator2.Value);
                 sortList3.Add(str1,dblVar);*/                
             }
         }

I want to store the enumerator2.key and value into sortList6, enumerator2.value needs to convert to double before add to sortList6, but I think my codes is wrong because it cannot work, any1 gt idea? Thank you.

Hey,

I don't know exactly what you're trying to do. But I tried to understand your code and came up with this:

public static void Display2()
    {
      // just some strings for testing purposes
      string[] exampleDB = new string[] { "this", "is", "a", "test" };

      // indexed List with lower memory usage of your dictionary
      List<string> sortList = new List<string>(exampleDB);

      // iterating every "key" with an "index"(in your case value)
      for (int i = 0; i < sortList.Count; i++)
      {
        // saving the string in tmp like --> (index):value
        String tmp = string.Format("({0}):{1}\n", i, sortList[i]);
        // writing down the string on the console
        Console.WriteLine(tmp);
      }
    }

Much likely a good resolution for your upcome. Why did you use the Enumeration or the Iterator or Dictionary? You just save simple indexes, don't you?

Thanks for reply, but that not what i wanted to get. Ok, i simply explains my problem:

1)I create a new IDictionary. => Dictionary<string, double> sortList1 = new Dictionary<string, double>();

2)IDictionaryEnumerator(enumerator2) to enumerate sortlist2 => sortlist2 contains keys and values

3)Problem is here: Wants add the enumerator2.Key and enumerator2.Value to the sortlist1 ( key is string, value is double)

I had tried many ways but keep stuck with this problem, hope some1 can help me..

So you simply want to mirror those two?
Why don't you just go like

sortList3 = sortList2

?

Or maybe something more likely to your approach:

foreach (KeyValuePair<string, double> keyValuePair in sortList2)
      {
        Console.WriteLine(string.Format("({0}):{1}\n", keyValuePair.Value, keyValuePair.Key));
        sortList3.Add(keyValuePair.Key,keyValuePair.Value);
      }
Dictionary<String, Double> sortList1 = new Dictionary<String, Double>(sortlist2);

Because sortlist2 is the sortlist before event. After i do event (enumerate with IDictionaryEnumerator), the new keys and values which i wanted to get already hold by enumerator2.Key and enumerator2.Value. My problem appeared here, i don't know the way to store those key and value (in enumerator) to sortList3..

There are three ways explained if you look above, so any questions left?

Here

IDictionaryEnumerator enumerator2 = sortList2.GetEnumerator();
 
             string str1 = String.Empty;
             while (enumerator2.MoveNext())
             {             
                 listbox.Items.Add("(" + enumerator2.Key.ToString() + "): " + " " + enumerator2.Value.ToString() + "\n");     

             sortList3.add(enumerator2.Key,enumerator2.Value);//here, i tried tis way, but gt error.. This is the problem
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.