Hashtable with 3 parameters

11,249

Solution 1

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

Solution 2

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

      }

Solution 3

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

Share:
11,249
asik
Author by

asik

Updated on June 04, 2022

Comments

  • asik
    asik almost 2 years

    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());  
        }
    }
    
  • asik
    asik over 12 years
    Hi parapura.. then how do I retrieve its value when I want to display in listview. because I have 3 columns in listview, phone number, name, address. how to separate the names and addresses on different columns
  • Olivier Jacot-Descombes
    Olivier Jacot-Descombes over 12 years
    I would add the phone number to the user struct (or class) as well, after all its part of the user information.