hashtables

if im holding a EndPoint as the key and a string as a value is there a way to get the value first and then return the key as a system.net.endpoint instead of the key returning as a object type

Answer this question

hashtables

  • johnzlin

    its kinda the way I was going to set it up but I think im going to just store the data in a types dataset cause I need to store a number of things, the main thing I wanted to do was this though

    its dealing with a udp connection

    in my receive void I pull the EndPoint out of the socket connected and was saving that as the hashtable key, then I checked to see if the hashtable contains that EndPoint if it does then it prossess the message if not then it adds the endpoint into the hashtable and that means the client is also sending a name so it stores the name as the value, but hashtables only return a object type unless you convert the object stored in the hashtable to a different type, I dont know of away to change a object type to System.Net.EndPoint, is why im going to save the ip as string and port as int to a dataset then it will be easy to create a EndPoint to use Socket.SendTo

    thanks for the help


  • Iced Earth

    What do you mean by "get the value first"

    Could you give some example code

    Jon


  • cmorel

    I think he wants to know the related key of his value in the hashtable.

    For instance, you have a hashtable that has this entry:

    key = 1

    value = a

    Given value a, he wants to know the corresponding key, which is 1 in this case.  But, my question is, why you want to do that

    What you can do, is loop over the VAlues collection of the hashtable, and, if the current value is the value you're looking for, get the key at this index:

     <code>

    object theKey = null;

    for( int i = 0; i < theHashtable.Values.Count; i++ )

    {

        if( theHashtable.Values [ i ] == theValueImLookingFor )

        {

             theKey = theHashtable.Keys [ i ];

            break;

        }

    }

     </code>

    However, I don't know how much you can rely on this construction, and, I also do not know why you want a reverse search Maybe it is simpler to have 2 hashtables then, one with objectA as key and objectB as value, and one with objectB as key, and objectA as value.  (This would of course mean that you'll have to maintain 2 hashtables, so that's a cost as well).



  • hashtables