Store data in a Hashtable

Hi

I get data from an cvs file and wuld like to store the data in a Hashtable, how does that work

I mange to store ("key","data") ~ ("001","John Doe")

but not ("001","John Doe","66539","KS","USA")

I will be greatfull for any useful hint about the problem.

//Matt

Sweden



Answer this question

Store data in a Hashtable

  • Tomas Galvez

    A hashtable stores key-value pairs. This means 1 key and 1 value. The value is of type Object, so you can store anything in there. In your case, you would have to create a class that contains the data for John Doe and add an instance of the class to the hash table

    public class Person

    {

    private string _name;

    private string _zip;

    private string _state;

    private string _country;

    public person()

    {

    }

    public string Name {

    get {return _name;}

    set {_name = value;}

    }

    public string Zip {

    get {return _zip;}

    set {_zip = value;}

    }

    public string State {

    get {return _state;}

    set {_state = value;}

    }

    public string Country{

    get {return _country;}

    set {_country = value;}

    }

    }

    To store the data in the Hashtable, use the following:

    HashTable table = new HashTable;

    Person p = new Person();

    p.Name = "John Doe";

    p.Zip = "66539";

    p.State = "KS";

    p.Country = "UA"

    able.Add("001", p);

    To retrieve the data from the HashTable, you'll have to cast the value to a Person object (as the value in the Hashtable is stored as type 'Object')

    Person p = (Person)HashTable["001"];



  • Store data in a Hashtable