Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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#?

  • *Experts*
Posted

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

"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
Posted

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.

Posted

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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...