Click here to Skip to main content
15,890,407 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
How can I make a hashTable with three parameters? I want to store phone numbers, names and addresses using a hashTable. Phone number as the key, and the name, address as its value. But I can put two data only, phone number and name. How do I get to save a phone number, name, address in the hashTable?

C#
Hashtable phoneBook;

public FrmPhoneBook()
{
    InitializeComponent();
    phoneBook = new Hashtable();
}

public void addNewPhoneBook(string name, string tel, string add)
{
    string names = name;
    string telp = tel;
    string address = add;

    if (!phoneBook.ContainsKey(telp))
    {
        phoneBook.Add(telp, names);
        getDetails();
    }
}

public void getDetails()
{
    lvDetails.Items.Clear();
    foreach (DictionaryEntry values in phoneBook)
    {
        lvDetails.Items.Add(values.Value.ToString());
        lvDetails.Items[lvDetails.Items.Count - 1].SubItems.Add(
           values.Key.ToString());  
    }
}
Posted

First of all, instead of Hashtable, use one of generic classes from <codey>System.Collections.Generic: <code>Dictionary (I would say, this is a candidate #1), SortedDictionary or SortedList. All these types are indexed with the key and provide computational complexity O<1> for the search by index; the difference is mostly the trade-off between speed and memory overhead. Non-generic type are rendered obsolete (for new development; formally, they are not obsolete) by introduction of generics in .NET 2.0. Type cast required by non-generic containers is a really bad thing.

Now, the answer to the question is really simple: create a structure or a class with the members for address and name; use this class as a value type, and the telephone as the key type as you did before.

—SA
 
Share this answer
 
Comments
Monjurul Habib 4-Dec-11 1:38am    
good recommendation. my 5!
Sergey Alexandrovich Kryukov 4-Dec-11 16:35pm    
Thank you, Monjurul.
--SA
you can use a List of string instead of a single string value. Try the ff:

C#
List<string> details = new List<string>();
list.Add(tel);
list.Add(add); 
</string></string>


and then use name or tel as key
C#
HashTable obj = new HashTable();
obj.Add(name, details);
//or obj.Add(tel, details);


Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
v3
Comments
upup7 4-Dec-11 1:03am    
then how to display in the listbox. when i want to display in the ListBox it's appear an error. for its key can appear, but for its value would not appear.


foreach (DictionaryEntry val in phoneBook)
{
listBox1.Items.Add("key :" + val.Keys + "and value : " + val.Values);
}


appear in the listbox like this:

key : 1 and value : System.Collections.Generic.List[System.String]
[no name] 4-Dec-11 1:11am    
you need to create another iteration to break down the values of the List. for ex:
string values = string.Empty;
foreach(string val in details.Items)
{
values += val + ", ";
}

then after getting the values from list, remove the trailing ","
upup7 4-Dec-11 6:26am    
then how if I want to edit one of its value. eg want to edit the name or address only. I've tried but its value changed all.

public void editValueName(string tel, string add)
{
string address = add;
string telp = tel;

if (phoneBook.ContainsKey(tel))
{
phoneBook[tel] = address;
getDetails();
}
}
C#

This is what I would do:
Create a new class: Contact, with three properties: Phone, Name, Address, and the rest of the code would be something like :
C#
public void addNewPhoneBook(string name, string tel, string add)
{
    var contact = new Contact(name, tel, add); 
    if (!phoneBook.ContainsKey(tel))
    {
        phoneBook.Add(tel, contact);
        getDetails();
    }
}

for getDetails ...
C#
public void getDetails()
{
    lvDetails.Items.Clear();
    lvDetails.Items = phoneBook.Values;
}

I did not compile this code but I think it must work.
It is better to have a Dictionary instead of a Hashtable, your dictionary could be defined as
C#
Dictionary<string, phoneBook> = new Dictionary<string, phoneBook>();
 
Share this answer
 
v2
//This code is Tested Code For Hash Table
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace HashTableDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // Create a new hash table.
        //
        Hashtable openWith = new Hashtable();
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string Hashkey = txtHashKey.Text;
            string HashVal = txtHashVal.Text;

            List<string> details = new List<string>();
            
            if (!openWith.ContainsKey(Hashkey))
            {
                details.Add(HashVal );
                openWith.Add(Hashkey, details);
            }
            else
            {
                foreach (DictionaryEntry values in openWith)
                {
                    string keyVal = Convert.ToString(values.Key);
                    if (keyVal == Hashkey)
                    {
                        List<string> addInHashValues = (List<string>)values.Value;
                        addInHashValues.Add(HashVal);
                    }
                }
            }
            rtbHash.Text = "";           
            getDetails();
        }

        public void getDetails()
        {
            foreach (DictionaryEntry values in openWith)
            {
                string keyVal = Convert.ToString(values.Key);
                rtbHash.Text += keyVal;
                List<string> details = (List<string>)values.Value;
                foreach (string testVal in details)
                {
                    rtbHash.Text += " "+ testVal;
                }
                rtbHash.Text += "\n";        
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            rtbHash.Text = ""; 
            getDetails();
        }
    }
}
 
Share this answer
 
v2

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