Hashtable and/or Dictionary in JScript

I don’t know C#, so I sometimes have a hard time translating the .NET C# examples into JScript.

I can create a hashtable with the following code, but can’t get a value out of it:

longLookup = new Hashtable();
	
for (i = 1; i < columnCount; i++) {
   longLookup.Add(temperatureData[0][i], i);
}

Debug.Log(longLookup.GetHash(90));  // 90 is a key in the hashtable

GetHash gives me an error: ‘System.Collections.Hashtable.GetHash’ is inaccessible due to its protection level.

What does that mean? How do I get a value corresponding to a particular key from a hashtable in JScript?

And what about a dictionary, which would probably be better for what I’m doing? I have no idea how to translate this to JScript:

Dictionary<string, string> openWith = new Dictionary<string, string>();

In general, is there anywhere to find the JScript syntax for .NET methods? I’ve searched and can’t find anything anywhere.

I believe it’s much simpler than that…

var test=new Hashtable();
test["myHash"]="This is a test";
print(test["myHash"]);

Regarding Dictionary usage:

var listing : Dictionary.<string, string> = new Dictionary.<string, string>();
listing.Add("somekey", "somevalue");
listing.Add("anotherkey", "anothervalue");

var value : String = listing["somekey"];

listing["somekey"] = "a new value";

At least that’s how I surmise it’s done in JavaScript from what I’ve seen.

There’s really no reason to use a Hashtable if you know the type of the objects you’re using. I’m not too sure what “temperatureData” is, but suppose it’s an int:

var longLookup : Dictionary.<int, int> = new Dictionary.<int, int>();

for (i = 1; i < columnCount; i++) {
   longLookup.Add(temperatureData[0][i], i);
}

Debug.Log(longLookup[90]);  // 90 is a key in the hashtable

Note that the key/value types for a Dictionary don’t have to be the same. For example, it could have an integer key but have a string value. You can also use object references for keys/values as well.

Debug.Log(longLookup[90]);

As for Dictionary:

C#:
Dictionary<string, string> openWith = new Dictionary<string, string>();
JS:
var openWith : Dictionary.<String, String> = Dictionary.<String, String>();

However both can be written with type inferencing instead which IMO is a lot more readable in this case:

C#:
var openWith = new Dictionary<string, string>();
JS:
var openWith = Dictionary.<String, String>();

You can use “new” in JS too if you want, but it’s optional and doesn’t change anything.

–Eric