Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static DateTime LoggedOn = DateTime.Now;



C#
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
Posted
Updated 17-Nov-16 23:14pm
v3

Just check if key and value exists:
C#
if(hash.ContainsKey("c") && hash["c"] == "3") { }
 
Share this answer
 
Comments
Member 11000455 18-Nov-16 5:18am    
in mycase i have done
if(ht.ContainsKey("Hello") && ht["Hello"] == LoggedOn.ToString())
{
//Do something here
}
but it is always false
Er. Puneet Goel 18-Nov-16 5:21am    
Check the solution i added below, that what you need
Member 11000455 18-Nov-16 5:25am    
if(hash.ContainsKey("c") && hash["c"] == "3") { } //yoursolution

if(ht.ContainsKey(message) && ht[message] == LoggedOn.ToString()) //same i have done but its same its not working

Member 11000455 18-Nov-16 5:22am    
also,

hash.Add("Hello", LoggedOn);
bool htexists = ht.ContainsKey("Hello");
bool valueexists = ht.ContainsValue(LoggedOn);

if (htexists && valueexists)
{
//It returns true which is corred as i have added key and value but i want to check if it is first time or no duplicate value how to checkt it
}
Er. Puneet Goel 18-Nov-16 5:23am    
if (hashtable.ContainsKey("Hello") && Convert.ToDateTime((hashtable["Hello"])) != Convert.ToDateTime(LoggedOn))
{
// Add value
}
You can also create extension method like below:

C#
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);
        }
    }
}
 
Share this answer
 
Comments
Member 11000455 18-Nov-16 5:45am    
ok Thankyou i have accepted but very simple question

ht.add("Hello")
// i want do do something if Hello is first in table entry Ok

which means
if(ht.containskey("Hello") but i want to check if there is no duplicate entry i know hastable throws error for duplicate values but is is possible to check before
Er. Puneet Goel 18-Nov-16 5:56am    
i don't where you are....

see if(ht.containskey("Hello") tell if it exists or not
so it tells you if its duplicate or not. YOu need to put this before adding as i mentioned in solution.
Member 11000455 18-Nov-16 6:00am    
Thanks its such a stupid mistake :-) i have made
Er. Puneet Goel 18-Nov-16 6:01am    
please give ratting to solution
Member 11000455 18-Nov-16 6:10am    
sure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900