Ian Lee
Ian Lee

Reputation: 91

Can't enumerate dictionary keyvaluepair

public partial class Form1 : Form
{
    public static Dictionary<string,string> contactList = new Dictionary<string,string>();
    public Form1()
    {
        InitializeComponent();
        Text = "My Telephone Directory";
    }

    private void txtAdd_Click(object sender, EventArgs e)
    {
        String name = txtName.Text;
        String teleNo = txtTelephone.Text;

        contactList.Add(name,teleNo);
        txtContactList.Text = "Added " + name;
    }

    private void txtClear_Click(object sender, EventArgs e)
    {
        txtContactList.Text = " ";
    }

    private void txtList_Click(object sender, EventArgs e)
    {

        String contactLists="";
        foreach (KeyValuePair<string,string> kvp in contactList)
        {

            contactLists += "Name: " + contactList.Keys.ToString()+ " Phone No: " + contactList.Values + Environment.NewLine;
        }
        txtContactList.Text = contactLists;
    }

    private void txtSearch_Click(object sender, EventArgs e)
    {

        String contactLists = "";


        foreach (KeyValuePair<string, string> kvp in contactList)
        {

            contactLists += "Name: " + contactList.Keys.ToString() + " Phone No: " + contactList.Values + Environment.NewLine;

            if (contactList.Keys.ToString() == txtName.Text)
            {

                contactLists += "Name: " + contactList.Keys.ToString() + " Phone No: " + contactList.Values.ToString() + Environment.NewLine;
            }
            txtContactList.Text = contactLists;

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
    }

}

I cant enumerate the dictionary, see the txtList_Click eventhandler.

If I do what im doing, I get System.Collections.Generic.Dictionary2+KeyCollection[System.String,System.String] Phone No: System.Collections.Generic.Dictionary2+ValueCollection[System.String,System.String].

If I do contactList.Key like im supposed to I get System.Collections.Generic.Dictionary

Upvotes: 0

Views: 124

Answers (2)

StuartLC
StuartLC

Reputation: 107237

Iterating the Dictionary and building the contactLists string

To iterate the Dictionary and build up a string, just use the KeyValuePair variable (kvp) that you've assigned in the foreach:

 foreach (KeyValuePair<string, string> kvp in contactList)
 {
        contactLists += "Name: " + kvp.Key 
                     + " Phone No: " + kvp.Value + Environment.NewLine;
        ...

However, concatenating the string contactLists in a loop is inefficient. StringBuilder would help here, or, instead of the loop, you can simply project out each key value pair "string" with a Linq Select, and then Join them.

var contactLists = 
    string.Join(Environment.NewLine,
        contactList.Select(cl => string.Format("Name: {0} Phone No: {1}",
                           cl.Key, cl.Value)));

Re searching the dictionary

One of the benefits of using a Dictionary (or HashSet) is that it offers indexed / keyed lookups, so you don't need to iterate the full collection to find an element by manual comparison of keys. So if you've keyed phone numbers by the person's name, this would be a more typical usage:

 var searchPhone = contactList[txtName.Text];

To make this more robust, you'd want to check if the key exists first, e.g. with ContainsKey or TryGetValue:

 string searchPhone;
 if (contactList.TryGetValue(text.Name, out searchPhone))
     ... found, use searchPhone
 else
    ... not found

Upvotes: 1

r31bs
r31bs

Reputation: 11

You are calling the ToString() methods on the collection objects rather then the specific key and value pair. Try this:

foreach (KeyValuePair<string,string> kvp in contactList)
{
    contactLists += "Name: " + kvp.Key + " Phone No: " + kvp.Value + Environment.NewLine;
}

Upvotes: 1

Related Questions