none
Get value from hash table

    Question

  • How would I get a value from a hash table from by using the key to find it?
    Sunday, December 06, 2009 10:52 AM

All replies

  • Hi,

    you can use the indexer of hashtable to get value.

    Here is a sample.

                Hashtable ht = new Hashtable();
                ht.Add(1, "Foo");
                ht.Add(2, "Foo2");
    
                object val = ht[2];
                string value = ht[1].ToString();
    And here is a useful link
    http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx
    Sunday, December 06, 2009 11:08 AM
  • Hmmm that's what I thought but doesn't seem to be working, what am I doing wrong?

            // Get user 
            public static TcpClient GetUser(string strUsername)
            {
                return (TcpClient)Server.htUsers[strUsername];
            }
    
            // Addusers
            public static void AddUser(TcpClient tcpUser, string strUsername)
            {
                Server.htUsers.Add(strUsername, tcpUser);
            }
    Sunday, December 06, 2009 11:13 AM
  • Hi,

    You are adding TcpClient's reference to the hashtable not its values, somehow in someline of your code if this tcpclient object is disposed you will not get value, because the object in hashtable also disposed.

    Here is a sample.

    Try this.

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Data source=.;initial catalog=Sample;integrated security=SSPI";
                Hashtable ht = new Hashtable();
                ht.Add("1", conn);
                Console.WriteLine(((SqlConnection)ht["1"]).ConnectionString.ToString());
                conn.Dispose();
                Console.WriteLine(((SqlConnection)ht["1"]).ConnectionString.ToString());//here comes empty string
    Sunday, December 06, 2009 11:20 AM
  • No it's not that and definately not getting disposed because this works to send data to all clients:

            public static void SendDataToAllClients(string Data)
            {
                StreamWriter swSenderSender;
                // Create an array of TCP clients, the size of the number of users we have
                TcpClient[] tcpClients = new TcpClient[Server.htUsers.Count];
                // Copy the TcpClient objects into the array
                Server.htUsers.Values.CopyTo(tcpClients, 0);
                // Loop through the list of TCP clients
                for (int i = 0; i < tcpClients.Length; i++)
                {
                    // Try sending a message to each
                    try
                    {
                        // If the message is blank or the connection is null, break out
                        if (Data.Trim() == "" || tcpClients[i] == null)
                        {
                            continue;
                        }
                        // Send the message to the current user in the loop
                        swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                        swSenderSender.WriteLine(Data);
                        swSenderSender.Flush();
                        swSenderSender = null;
                    }
                    catch // If there was a problem, the user is not there anymore, remove him
                    {
                        RemoveUser(tcpClients[i]);
                    }
                }
            }

    I just can't work out from this how to get a single client by it's key, and what I posted doesn't appear to work.
    Sunday, December 06, 2009 11:27 AM
  • Hi,

    Might this be the problem you are experiencing , I can not reproduce your problem, could you send the line where you call AddUser and GetUser

            static Hashtable htUsers = new Hashtable();
            // Get user 
            public static TcpClient GetUser(string strUsername)
            {
                return (TcpClient)htUsers[strUsername];
            }
    
            // Addusers
            public static void AddUser(TcpClient tcpUser, string strUsername)
            {
                htUsers.Add(strUsername, tcpUser);
            }
    
            static void Main(string[] args)
            {
                AddUser(new TcpClient(), "tamer");
                TcpClient t=GetUser("tamer");
                TcpClient t1 = GetUser("Tamer");//does return null because the key is case sensitive
                Console.Read();
    
    
    
    Sunday, December 06, 2009 11:52 AM