Jump to content

Hashtable


Gauls

Recommended Posts

Hi i have two hashtablesexample :Hashtable 1 Hashtable 2key Value key ValueDetails Order Details ProductCan i get One Order from 1st hashtable and then One product from the second hashtable ?Thanks

Link to comment
Share on other sites

I am not sure if I understood the question correctly, To get the value from the hashtables....Dim key = "2"          'some numberResponse.Write(hashtable1(key))Response.Write(hashtable2(key))

My Question is Incase if i have two hashTables and does not contain same key values , so there is no link between the two tables right. Now i want to loop thru both the hashtables at a time like one value from 1st hashtable and one value from the 2nd hashtable then move next ....so on Can i use for i as integer=0 to ht.count or something like that ??
Link to comment
Share on other sites

Try this code, this will give you the complete list of keys and its corresponding values...Dim myEnumerator As IDictionaryEnumerator = hashtablename.GetEnumerator()While myEnumerator.MoveNext()Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " + myEnumerator.Value.ToString())End While

Link to comment
Share on other sites

you can do it 2 ways

Hashtable ht = new Hashtable();ht.Add("one","1");ht.Add("two","2");ht.Add("three","3");string[] vals = new string[ht.Count];ht.Values.CopyTo(vals,0);for(int i=0;i<keys.Length;i++){	Response.Write(vals[i].ToString());}

Hashtable ht = new Hashtable();ht.Add("one","1");ht.Add("two","2");ht.Add("three","3");string[] keys = new string[ht.Count];ht.Keys.CopyTo(keys,0);for(int i=0;i<keys.Length;i++){	Response.Write(ht[keys[i]].ToString());}

For whatever reason when the Hashtable is copied it is copied backwards. You will see what I mean when you run the code.You can fix that by running hte for loop like this

for(i=keys.Length-1;i>=0;i--){         //i didn't test this part so it might be off a bit}

EDIT: oops I didn't see pulpfictions post. His solution looks cleaner.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...