Jedhi Posted February 13, 2004 Posted February 13, 2004 How do I add information other than key and value to a hashtable ?? Let say that my key is security number and value is the name. If I am interesting in adding additional information to the key like the address, how can I do this in C#? Quote
*Experts* Nerseus Posted February 14, 2004 *Experts* Posted February 14, 2004 Well first, a hashtable is just a quick way to associate keys to values and retrieve them. If you want to store multiple fields, you probably want something more advanced - like a DataSet. But if you really want to use the hashtable, you could convert both the security number and the address to strings, and use the concatenated string as the key. I can't think of a practical use for that, but who knows? -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* Derek Stone Posted February 14, 2004 *Gurus* Posted February 14, 2004 Hashtables store objects, including string values. If you want to store both a name and an address create a "Person" object and add that to the hashtable instead of a string. Quote Posting Guidelines
Jedhi Posted February 14, 2004 Author Posted February 14, 2004 Could you give me an example in C# of how you would do it if you had tree issues like security number as key, name as value and an address that is related to the key. And an example of doing it with dataset. I would appreciate it very much. Quote
Rodenberg Posted February 15, 2004 Posted February 15, 2004 First create an object to represent the "Person": internal sealed class Person{ private string socialSecurityNumber; private string name; private string address; private bool isAlive; public Person(string ssNum, string theName, string theAddress, bool aliveStatus){ this.socialSecurityNumber = ssNum; this.name = theName; this.address = theAddress; this.isAlive = aliveStatus; } public string Name{ get{ return this.name; } } public string Address{ get{ return this.address; } } public string SocialSecurityNumber{ get{ return this.socialSecurityNumber; } } public bool IsAlive{ get{ return this.isAlive; } } } ********************************************* The next step is to create however many person object instances as you need, and store each in a hashtable using the social security number as the unique identifier (IE Key). public sealed class DemoClass{ private Person person; private Hashtable table = new Hashtable(); public DemoClass(){} public void CreateUser(string name, string address, string socialSecurityNumber, bool isAlive){ person = new Person(name,address,socialSecurityNumber, isAlive); this.table.Add(person.SocialSecurityNumber,person); } public Person GetPerson(string socialSecurityNumber){ if(!table.Contains(socialSecurityNumber) return null; ///PERSON DOESNT EXIST ///PERSON INSTANCE EXISTS, CAST OBJECT INSTANCE INTO PERSON BEFORE RETURNING return (Person)table[socialSecurityNumber]; } } Hope this clears a few things up for you, good luck! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.