asik
asik

Reputation: 209

Hashtable with 3 parameters

How can I make a hashTable with three parameters? I want to store phone numbers, names and addresses using a hashTable and a dictionary. 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?

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());  
    }
}

Upvotes: 6

Views: 12465

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

Put all your user data into a class:

public class User
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
}

Then process as follows:

Dictionary<string, User> reverseLookUp = new Dictionary<string, User>();
User user;

// Fill dictionary
user = new User { Name = "John", Address = "Baker Street", PhoneNumber = "012345" };
reverseLookUp.Add(user.PhoneNumber, user);
user = new User { Name = "Sue", Address = "Wall Street", PhoneNumber = "333777" };
reverseLookUp.Add(user.PhoneNumber, user);

// Search a user
string phoneNumber = "012345";
if (reverseLookUp.TryGetValue(phoneNumber, out user)) {
    Console.WriteLine("{0}, {1}, phone {2}", user.Name, user.Address, user.PhoneNumber);
} else {
    Console.WriteLine("User with phone number {0} not found!", phoneNumber);
}

// List all users
foreach (User u in reverseLookUp.Values) {
    Console.WriteLine("{0}, {1}, phone {2}", u.Name, u.Address, u.PhoneNumber);
}

You could also create a specialized dictionary for that purpose:

public class PhoneDict : Dictionary<string, User>
{
    public void Add(User user)
    {
        Add(user.PhoneNumber, user);
    }
}

Then add users as follows:

PhoneDict phoneDict = new PhoneDict();
User user;

// Fill dictionary
user = new User { Name = "John", Address = "Baker Street", PhoneNumber = "012345" };
phoneDict.Add(user);
user = new User { Name = "Sue", Address = "Wall Street", PhoneNumber = "333777" };
phoneDict.Add(user);

Upvotes: 4

Surjit Samra
Surjit Samra

Reputation: 4662

You can use Tuple if your are using .NET 4.0 and above

Dictionary<string, Tuple<string, string>> myHash = new Dictionary<string, Tuple<string, string>>();

from MSDN

A tuple is a data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) that is used to store an identifier such as a person's name in the first element, a year in the second element, and the person's income for that year in the third element.

Here is code sample you can use

class Program
  {
    static void Main(string[] args)
    {

      Dictionary<string, Tuple<string, string>> myHash = new Dictionary<string, Tuple<string, string>>();

      //Test with 10 records

      //Create 10 records
      Enumerable.Range(1, 10).All(a => { myHash.Add("12345" + a.ToString(), new Tuple<string, string>("user" + a.ToString(), "user" + a.ToString() + "address")); return true; });

      //Display 10 records
      myHash.Keys.All(a => { Console.WriteLine(string.Format("Key/Phone = {0} name = {1} address {2}", a, myHash[a].Item1, myHash[a].Item2)); return true; });

      Console.ReadLine();

    }
  }

Further Tuples are commonly used in four ways:

  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.

  • To provide easy access to, and manipulation of, a data set.

  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).

  • To pass multiple values to a method through a single parameter.

Under the hood it uses Factory pattern to instantiate relative structure

Upvotes: 1

parapura rajkumar
parapura rajkumar

Reputation: 24403

You can have the key as the phone number and the value as a struct which has two members one being the address and one being the name. Also consider moving to Dictionary as it is typesafe

        struct User
        {
            public string Name;
            public string Address;
        }

       static void Main(string[] args)
       {
           Dictionary<string, User> hash = new Dictionary<string, User>();

          //To add to the hash
           hash.Add( "22255512282" , 
                new User(){ Name = "foo" , Address = "Bar" });

          //To lookup by key
          User user;
          if (hash.TryGetValue("22255512282", out user))
          {
             Console.WriteLine("Found " + user.Name);
          }

      }

Upvotes: 5

Related Questions