Hashtable with three parameters

How can I make hashTable with three parameters? I want to store phone numbers, names and addresses using hashTable and dictionary . Phone number as a key, and name, address - its value. But I can specify only two data, phone number and name. How to get phone number, name, address in 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()); } } 
+6
source share
3 answers

Put all your user data in 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 can also create a specialized dictionary for this 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); 
+4
source

You can have a key as a phone number, and a value as a structure, in which two members are an address and one is a name . Also consider moving to the Dictionary , as it is typeafe

  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); } } 
+5
source

You can use Tuple if you are using .NET 4.0 or higher

 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), which is used to store an identifier, such as the name of the person in the first element, the year in the second element and the person’s income for that year in the third element.

Here is an example code 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(); } } 

Extra tuples are usually used in four ways:

  • Present a single dataset. For example, a tuple may be a database record, and its components may be separate record fields.

  • To provide easy access to the data set and its manipulation.

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

  • Pass multiple values ​​to a method with a single parameter .

Under the hood, the Factory pattern is used to create a relative structure.

+1
source

Source: https://habr.com/ru/post/902993/


All Articles