Help with hashing

Hello guys, is there anyone there who knows how to put the hash.Keys and hash.Values to a label?

I have this code but doesn’t work:

GUI.Label(new Rect(10, 40, 300, 30), "Key: " + hash.Keys + " Value: " + hash.Values);

I also have this code which supposed to print those too:

print("Hashkey: " + hash.Keys + " Hashvalue: " + hash.Values);

but the result is:

Hashkey: System.Collections.Hashtable+Hashkeys Hashvalue: System.Collections.Hashtable+Hashvalues

What could be the problem with this? Thanks.

A Hashtable can contain any number of key-value pairs. Therefore, Keys and Values are arrays enumerating all keys and values in the table:

Thanks for replying tom, okay, I used

 foreach(object key in hashX.Keys)
                {
                    print("Hashkey: " + key + " Hashvalue: " + hashX.Values);
                }

but still the same result.

the key that I put on my hashtable is an incrementing x, five iterations, and the value for each key is the hit.point.x when I’m raycasting.
I wanted to print each key and value in the console, but it doesn’t work. :frowning:

you may take a look at that part of my code.

Rect rect = new Rect(10, 10, 120, 30);

        if (GUI.Button(rect, "Raycast"))
        {
            Random.seed = 1234;
            for (int x = 0; x < 5; x++)
            {
                RayCasting();

                hashX.Add(x, arrayListX[x]);

                foreach(object key in hashX.Keys)
                {
                    print("Hashkey: " + key + " Hashvalue: " + hashX.Values);
                }
            }
        }

the arrayListX[ ] is the arrayList where I put my hit.point.x(which is a random float) after every raycast.

What doesn’t work exactly? Does it print the key correctly now? Because that part seems ok. It’s just that you still use the array Values instead of it’s contents. You cannot really use foreach to iterate over two arrays like this. To iterate over both, try:

foreach (DictionaryEntry dictEntry in hashX)
    print("key: " + dictEntry.Key + " value: " + dictEntry.Value);

Also, why are you using a hashtable? ‘x’ is an int ranging from 0 to 5, so you might as well use arrayListX directly. Hashtables are meant for arrays that cannot use 0-n integers as index.

actually, I tried both first but it doesn’t work. this is my previous code : print("Hashkey: " + hashX.keys + " Hashvalue: " + hashX.Values);

and this is the result which is being printed 5times.

Hashkey: System.Collections.Hashtable+Hashkeys Hashvalue: System.Collections.Hashtable+Hashvalues

I also tried your code

foreach (DictionaryEntry dictEntry in hashX)
    print("key: " + dictEntry.Key + " value: " + dictEntry.Value);

Still the same result.

I’d suggest making a separate test script, just to try Hashtable functionality. Something like this:

Hashtable ht = new Hashtable();
ht["monkey"] = "banana";
ht["dog"] = "bone";

foreach (DictionaryEntry dictEntry in ht)
    print("key: " + dictEntry.Key + " value: " + dictEntry.Value);

This code should work (i compiled and tested it in the Start() of an otherwise empty monoBehaviour), but if it doesn’t, there is something seriously wrong in your Mono build.

However, I believe it’s most likely something is wrong somewhere else in your code (i.e. somewhere beyond the hashtable code you pasted in this thread).