Deena eliz

Deena eliz

  • NA
  • 50
  • 19.8k

changing hashtable value of particular key in c#

May 8 2015 3:22 AM
HI everyone,,

This is my code for add data into  hashtable and modify particulat value..


Hashtable HT = new Hashtable();
        int val = 0;
       

        private void button1_Click(object sender, EventArgs e)
        {
            
           
            HT.Add(textBox1.Text,val);
            HT.Add(textBox2.Text, val);
            HT.Add(textBox3.Text, val);
            HT.Add(textBox4.Text, val);

            foreach (DictionaryEntry de in HT)
            {
                MessageBox.Show(de.Key + " " + de.Value);
            }

        }

 i have added some values in hashtable as above code.. if i  add same data again it should check with all the keys. if it is exist, value should be increase.. i did this some wat as below code


 int val1 = 0;
          
            if (HT.ContainsKey(textBox5.Text))
            {
                HT[textBox5.Text] = ++val;
              
               
            }

            else if (!HT.ContainsKey(textBox5.Text))
            {
                HT.Add(textBox5.Text,val1);
             
            }
            foreach (DictionaryEntry de in HT)
            {
                MessageBox.Show(de.Key + " " + de.Value);

            }


but am getting wrong answer.. if i add same key, the value should increase as 1.. then i add another same key the value should come as also 1(0+1)  but its showing 2 (1+1)


please anyone clear my doubt..
thanks in advance..

Attachment: globaldata.rar