locked
hashtable question RRS feed

  • Question

  • hello,
    i want to update a value in a hashtable (ht) so i wrote-

    ht["key1"]=Convert.ToInt32(ht["key1"])+1

    but the value is remaining the same
    what im missing?

    thanks in advanced

    Saturday, May 21, 2005 9:12 AM

Answers

  • Hi,

    I just pasted your code and tried and it is working as expected. The value is getting incremented. Full snippet for the code which I tried:


    Hashtable ht = new Hashtable();
    ht[
    "key1"] = 2;
    ht[
    "key1"] = Convert.ToInt32(ht["key1"]) + 1;
    MessageBox.Show(ht["key1"].ToString());


     



    Please verify if you have checked the initial value correctly.

    Regards,
    Vikram

    Saturday, May 21, 2005 6:36 PM
  • The initial value could be zero. Check initial value of (dr["id"]).
    Tuesday, May 24, 2005 8:09 PM

All replies

  • Hi,

    I just pasted your code and tried and it is working as expected. The value is getting incremented. Full snippet for the code which I tried:


    Hashtable ht = new Hashtable();
    ht[
    "key1"] = 2;
    ht[
    "key1"] = Convert.ToInt32(ht["key1"]) + 1;
    MessageBox.Show(ht["key1"].ToString());


     



    Please verify if you have checked the initial value correctly.

    Regards,
    Vikram

    Saturday, May 21, 2005 6:36 PM
  • ok so i tried that and its working but what in trying is to copy fields from a dataTable (in dataSet) to a hashTable
    here is the code-
    foreach(DataRow dr in DS.Tables["table1"].Rows) 
    {

    if(hashTabel1.ContainsKey(Convert.ToString(dr["id"])))

    {

    int x=Convert.ToInt32(hashTable1["id"]);

    Label1.Text=x.ToString(); //the output here is 0 , why?????????

    }

    else

    {

    hashTablw1.Add(Convert.ToString(dr["id"]),1); 

    }

    }

    the output supposed to be 1 not 0
    whta is the problem???
    whanks in advanced

    Tuesday, May 24, 2005 9:30 AM
  • The initial value could be zero. Check initial value of (dr["id"]).
    Tuesday, May 24, 2005 8:09 PM
  • why dr["id"]?
    but i put 1 in ht["ïd"] ...

    Wednesday, May 25, 2005 7:28 AM
  • nevermind...i get that
    Friday, May 27, 2005 7:44 PM