本文介绍了如何将检查键添加到hastable及其对应的值中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static DateTime LoggedOn = DateTime.Now;
Hashtable ht = new Hashtable();
hash.Add("Hello", LoggedOn); // output like "Hello", 18.11.2016 09:52:07
hash.Add("Hello", LoggedOn); // output like "Hello", 18.11.2016 09:52:27
hash.Add("Hello", LoggedOn); // output like "Hello", 18.11.2016 09:52:55
bool htexists = ht.ContainsKey("Hello");
bool valueexists = ht.ContainsValue(LoggedOn);
if (htexists && valueexists)
{
//if it is first entry allow it
//my first question it returns always true as i have addaded key and value to my ht but i want to check when it is added more than one time of not if first entry OK
// my second ? how to check then correspondend value in my case with time
//if same message say hello is added within few minutes then cancel of say only allowes to add same message during interval of 1 hour or 1 day
}
What I have tried:
First i would like to check if value already exists or not i have done something like this
推荐答案
if(hash.ContainsKey("c") && hash["c"] == "3") { }
public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Hashtable hashtable = new Hashtable();
hashtable.AddIfNotExists("Time1", Convert.ToDateTime("11/18/2016 3:43:33 PM"));
hashtable.AddIfNotExists("Time2", Convert.ToDateTime("11/18/2016 3:43:34 PM"));
hashtable.AddIfNotExists("Time3", Convert.ToDateTime("11/18/2016 3:43:33 PM"));
hashtable.AddIfNotExists("Time4", Convert.ToDateTime("11/18/2016 3:43:35 PM"));
}
}
public static class Extension
{
public static void AddIfNotExists(this Hashtable hash, string key1, DateTime value2)
{
if (hash.ContainsKey(key1) == false && Convert.ToDateTime((hash[key1])) != value2)
{
hash.Add(key1, value2);
}
}
}
这篇关于如何将检查键添加到hastable及其对应的值中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!