Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all,
I would like to display elements of a dictionary(string,double). Here is the code:
C#
private void populateDictionary()
{
    g_masses.Add("K", 128.0949626);
    g_masses.Add("R", 156.1011106);
    g_masses.Add("S", 87.0320282);
    g_masses.Add("L", 113.0840636);
    g_masses.Add("G", 57.0214636);
    g_masses.Add("T", 101.0476782);
    g_masses.Add("V", 99.0684136);
    g_masses.Add("A", 71.0371136);
    g_masses.Add("P", 97.0527636);
    g_masses.Add("E", 129.0425928);
    g_masses.Add("N", 114.0429272);
    g_masses.Add("Q", 128.0585772);
    g_masses.Add("D", 115.0269428);
    g_masses.Add("Y", 163.0633282);
    g_masses.Add("H", 137.0589116);
    g_masses.Add("C", 103.0091843);
    g_masses.Add("F", 147.0684136);
    g_masses.Add("W", 186.0793126);
    g_masses.Add("M", 131.0404843);
    g_masses.Add("waterMass", 18.01056);
    g_masses.Add("hydrogen", 1.00785);
}


I would like to display the string value in a column and the double value in the next column. Any suggestions? Thanks!
Posted
Updated 4-Jun-11 5:21am
v2

This should do it -
foreach (KeyValuePair<string,double> i in g_masses)
{
     Console.WriteLine(i.Key, i.Value);
}
 
Share this answer
 
v2
Comments
shekarchee 4-Jun-11 11:33am    
Thanks everyone!
Abhinav S 4-Jun-11 11:34am    
You are welcome.
Sergey Alexandrovich Kryukov 5-Jun-11 2:11am    
Sure, there are two variants, this is one, a 5.
--SA
Abhinav S 5-Jun-11 4:08am    
Thank you SA.
C#
private Dictionary<String,Double> g_masses = new Dictionary<String, Double>()
...
private void populateDictionary()
{
    g_masses.Add("K", 128.0949626);
    g_masses.Add("R", 156.1011106);
    g_masses.Add("S", 87.0320282);
    g_masses.Add("L", 113.0840636);
    g_masses.Add("G", 57.0214636);
    g_masses.Add("T", 101.0476782);
    g_masses.Add("V", 99.0684136);
    g_masses.Add("A", 71.0371136);
    g_masses.Add("P", 97.0527636);
    g_masses.Add("E", 129.0425928);
    g_masses.Add("N", 114.0429272);
    g_masses.Add("Q", 128.0585772);
    g_masses.Add("D", 115.0269428);
    g_masses.Add("Y", 163.0633282);
    g_masses.Add("H", 137.0589116);
    g_masses.Add("C", 103.0091843);
    g_masses.Add("F", 147.0684136);
    g_masses.Add("W", 186.0793126);
    g_masses.Add("M", 131.0404843);
    g_masses.Add("waterMass", 18.01056);
    g_masses.Add("hydrogen", 1.00785);
}

...

foreach(String key in g_masses.Keys)
{
    Double value = g_masses[key];
    // Do something with String key and Double value
}
...


Column layout is up to you!

Cheers!
 
Share this answer
 
Comments
Abhinav S 4-Jun-11 11:34am    
Good answer. My 5!
I've used a KeyValuePair instead.
Sergey Alexandrovich Kryukov 5-Jun-11 2:12am    
This is another one, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900