Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Dictionary Class example

Back to Forum: Tech Off
  • User profile image
    Shark_M

    what is the typical uses for a dictionary class?

    can i use it for adding Asynchronus sockets, with a Key string?

    what is the difference between this and ArrayLists, and HashTables?

    which one should i use

    if i want to use the collection to find or relate a UserID with a Socket

    can you show me an example? i am beginner in C#

    thanks alot

  • User profile image
    TommyCarlier

    What do you mean by dictionary class? Is it the generic Dictionary<K,V> from .NET 2.0? Or something from .NET 1.1?

    If you're using .NET 2.0, you can use the generic Dictionary class like this:
    Dictionary<string, Socket> myDictionary = new Dictionary<string, Socket>();
    myDictionary["socket1"] = mySocket1;
    myDictionary["socket2"] = mySocket2;
    Socket findSocket = myDictionary["socket1"];

    If you're using .NET 1.1, you could use the Hashtable class (which implements IDictionary) like this:
    Hashtable myDictionary = new Hashtable();
    myDictionary["socket1"] = mySocket1;
    myDictionary["socket2"] = mySocket2;
    Socket findSocket = myDictionary["socket1"] as Socket;

  • User profile image
    Dr Herbie

    Shark_M wrote:
    what is the typical uses for a dictionary class?


    I usually use it for speed when searching for objects.  Instead of putting objects into an ArrayList and then searching for a specific one by looping through the collection, I put the objects into the dictionary, using the search criteria as the key.  I can then quickly get the relevant item.

    You will find that this is only faster when there are more than a certain number of items to be stored.  You might want to experiment with ArrayLists, HashTables, or even System.Collections.Specialized.HybridDictionary and see which one is faster.

    In addition to speed, you will find that the code is a little simpler using:

    object = dictionary[key]

    as opposed to

    foreach(object in list)
    {
        if(object.searchcriteria == key)
           return object;
    }


  • User profile image
    Shark_M

    i have made a state object

    public class StateObject
            {
                // Client  socket.
                public Socket workSocket = null;
                private string _SocketUserID;
                public string SocketUserID
                {
                    get
                    {
                        return _SocketUserID ;
                    }
                    set
                    {
                        _SocketUserID = value;
                    }
                }
                // Size of receive buffer.
                public const int BufferSize = 1024;
                // Receive buffer.
                public byte[] buffer = new byte[BufferSize];
                // Received data string.
                public StringBuilder sb = new StringBuilder();
            }

    i also have a _SocketUserID

    i want the SocketUserID to be uniuqe for a given stateobject (holds a socket for a given connection with a client)

    then i pass this to the dictionary class

    <UserID string (its a GUID), stateobject>

    the UserID string = StateObject.SocketUserID


    so when i try to send a message to a given client,
    i find the socket associated with the client using the UserID string

    so i did this

    Dictionary<string, Socket> UserIDSocketDictionary = new Dictionary<string, Socket>()



    this.UserIDSocketDictionary[ClientSocketState.SocketUserID] = ClientSocketState.workSocket;


    would this work ok?

  • User profile image
    TommyCarlier

    Yes, that would work fine in .NET 2.0.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.