Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Hashtable value of particular key [closed]

Tags:

c#

.net

When user click on confirm and review button respactive(key ,value) pair is getting stored in hashtable,when user click on review button than that particular list item elments color is red ,what I want is if (key,value) pair is added using review button than I want to change its value if user clicks on confirm button ,In short in this (key ,value) pair I am storing (question,answer) so if user is not sure about answer than he click on review ,and later he should be able to change its answer and mark as confirm so this list item elements color gets change to green ,how can i do this

 private void AddtoHashTabl(string key, string value)
            {
                if (hashtable.ContainsKey(key))
                {

                }
                else
                {
                    hashtable.Add(key, value);
                }
            }
    private void Confirm_Click(object sender, EventArgs e)
            {
                string Q = "";
                string A = "";
                listView1.Items[Convert.ToInt16(listView1.SelectedItems[0].SubItems[0].Text) - 1].BackColor = Color.Green;

                var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
                var selectedQuestion = questions[q - 1];
                Q = selectedQuestion.Id;
                if (radioButton12.Checked == true)
                    A = "1";
                else if (radioButton11.Checked == true)
                    A = "2";
                if (radioButton10.Checked == true)
                    A = "3";
                if (radioButton9.Checked == true)
                    A = "4";

                AddtoHashTabl(Q, A);
            }
    private void Review_Click(object sender, EventArgs e)
            {
                string Q = "";
                string A = "";
                listView1.Items[Convert.ToInt16(listView1.SelectedItems[0].SubItems[0].Text) - 1].BackColor = Color.Red;

                var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
                var selectedQuestion = questions[q - 1];
                Q = selectedQuestion.Id;
                if (radioButton12.Checked == true)
                    A = "1";
                else if (radioButton11.Checked == true)
                    A = "2";
                if (radioButton10.Checked == true)
                    A = "3";
                if (radioButton9.Checked == true)
                    A = "4";

                AddtoHashTabl(Q, A);
            }

Thanks in advance for Any help

like image 425
Durga Avatar asked Aug 16 '13 07:08

Durga


1 Answers

Just use the indexer:

hashtable[key] = value;

That will set or add a value - so you can replace any AddtoHashTabl calls with just that indexer setter call.

Note that you'd be better off using a generic collection such as Dictionary<TKey, TValue> if at all possible. The non-generic collections are rarely useful these days.

EDIT: To know where the value came from the review or confirm button, I would suggest having a separate collection (e.g. a HashSet<string>) of "confirmed responses". Or even simply two separate hashtables, one for each button. Alternatively, you could have an Answer class which contained both the value and its status as confirmed or not.

like image 140
Jon Skeet Avatar answered Oct 14 '22 15:10

Jon Skeet