【问题标题】:Putting restrictions on deleting hashtable限制删除哈希表
【发布时间】:2014-09-12 19:39:59
【问题描述】:

我的表单中有一个哈希表。所以基本上我有 2 个按钮 AddDelete。当我将信息放入文本框中并添加时,将其添加到哈希表中。但是当我单击删除时,它会删除它,并且当其中没有值时,它会显示错误。 问题:所以我想要做的是,当我将信息放入 textBox1 中时,它没有添加到 Hashtable 中,它应该会出错,否则如果该值已经添加,它应该删除它。

    public Form1()
    {
        InitializeComponent();
    }
    Hashtable Info = new Hashtable();

    private void button1_Click(object sender, EventArgs e)
    {
        string a = textBox1.Text;
        string b = textBox2.Text;
        if (a == "" && b == "" || a == "" || b == "")
        {
            MessageBox.Show("Missing Input!");
        }
        else
        {
            MessageBox.Show("Added successfully");
            label4.Text = a + " " + b;
        }

    }

    private void button4_Click(object sender, EventArgs e)
    {

        string a = textBox1.Text;
        string b = textBox2.Text;
        if (a == "")
        {
            MessageBox.Show("Missing Value");
        }
        else if(Info.ContainsKey(a)) // but this deletes it even if the value has not been added
        {
            MessageBox.Show(textBox1.Text + "has been removed");
            Info.Remove(a);
        }

    }

例如:如果我在Hashtable中添加2并尝试删除3,它仍然会因为textBox中有一些值而删除它。

【问题讨论】:

  • 您能否重构您的代码以使用 string.IsNullOrEmpty() 方法也可以单独包装您的 Or 条件,我认为考虑在这种情况下的操作顺序.. 还有你为什么要在这里重复分配 @987654322 @ 在你的 else 语句中..?
  • 在这一行else if(Info.ContainsKey(a)) // but this deletes it even if the value has not been added 为什么不告诉我们确切的值是什么我的猜测是你没有使用调试器你确定你没有在.Contains()方法
  • Hint ` label4.Text = a + " " + b;` 如果你有一个像Mary had a little lamb 这样的字符串,它会删除它,因为它包含我所看到的“”..你能给请举个例子或显示您的哈希表中的内容
  • Info.Add(...) 在哪里?
  • 好的,所以我有 2 个文本框,一个用于数字,另一个用于名称。例如 - 数字 = 1 和名称 = 山姆。所以现在当我尝试删除数字 1 时,它会删除数据,但是当我在数字文本框中输入 2 而不是说没有与此数字相关的数据时。它只是给出一条数据已被删除的消息

标签: c# winforms hashtable


【解决方案1】:
public Form1()
{
    InitializeComponent();
}
Hashtable Info = new Hashtable();

private void AddToHashTable_Click(object sender, EventArgs e)
{
    string a = textBox1.Text;
    string b = textBox2.Text;
    if (a == "" || b == "")
    {
        MessageBox.Show("Missing Input!");
    }
    else if(Info.ContainsKey(a) || Info.ContainsKey(b))
    {
       MessageBox.Show("Hash table already contain this key");
    }
    else
    {
        Info.Add(a);
        Info.Add(b);
        MessageBox.Show("Added successfully");
        label4.Text = a + " " + b;
    }

}

private void DeleteFromHashTable_Click(object sender, EventArgs e)
{

    string a = textBox1.Text;
    string b = textBox2.Text;
    if (a == "")
    {
        MessageBox.Show("Missing Value");
    }
    else if(Info.ContainsKey(a)) // but this deletes it even if the value has not been added
    {
        MessageBox.Show(a + " has been removed");
        Info.Remove(a);
    }
    else
    {
        MessageBox.Show(a + " is not part of the hash table");
    }

    //same check here for b
    if (b == "")
    {
        MessageBox.Show("Missing Value");
    }
    else if(Info.ContainsKey(b)) // but this deletes it even if the value has not been added
    {
        MessageBox.Show(b + " has been removed");
        Info.Remove(b);
    }
    else
    {
        MessageBox.Show(b + " is not part of the hash table");
    }


}

我稍微更改了代码。你错过了一些重要的事情。就像在尝试添加键时检查键是否已存在于 HashTable 中。如果您尝试添加现有密钥,则会发生异常。我还更改了方法的名称,您错过了在哈希表中添加两个文本框。

【讨论】:

    猜你喜欢
    • 2013-09-17
    • 2021-06-01
    • 2023-04-10
    • 2018-11-17
    • 2017-07-17
    • 2012-11-15
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    相关资源
    最近更新 更多