C# 更改特定键的哈希表值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18267896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:44:28  来源:igfitidea点击:

Changing Hashtable value of particular key

c#.net

提问by Durga

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

在此先感谢您的任何帮助

采纳答案by Jon Skeet

Just use the indexer:

只需使用索引器:

hashtable[key] = value;

That will set oradd a value - so you can replace any AddtoHashTablcalls with just that indexer setter call.

这将设置添加一个值 - 因此您可以AddtoHashTabl仅使用该索引器 setter 调用替换任何调用。

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.

请注意,Dictionary<TKey, TValue>如果可能的话,最好使用泛型集合。如今,非通用集合很少有用。

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

编辑:要知道或确认按钮的值来自哪里,我建议有一个单独的集合(例如一个HashSet<string>)“已确认的响应”。或者甚至只是两个单独的哈希表,每个按钮一个。或者,您可以拥有一个Answer包含已确认或未确认的值及其状态的类。