Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hashtable and/or Dictionary in JScript

Discussion in 'Scripting' started by John-B, Jan 24, 2011.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    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:

    Code (csharp):
    1. longLookup = new Hashtable();
    2.    
    3. for (i = 1; i < columnCount; i++) {
    4.    longLookup.Add(temperatureData[0][i], i);
    5. }
    6.  
    7. 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:

    Code (csharp):
    1. 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.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I believe it's much simpler than that...

    Code (csharp):
    1.  
    2. var test=new Hashtable();
    3. test["myHash"]="This is a test";
    4. print(test["myHash"]);
    5.  
     
  3. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Regarding Dictionary usage:

    Code (csharp):
    1. var listing : Dictionary.<string, string> = new Dictionary.<string, string>();
    2. listing.Add("somekey", "somevalue");
    3. listing.Add("anotherkey", "anothervalue");
    4.  
    5. var value : String = listing["somekey"];
    6.  
    7. 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:

    Code (csharp):
    1. var longLookup : Dictionary.<int, int> = new Dictionary.<int, int>();
    2.  
    3. for (i = 1; i < columnCount; i++) {
    4.    longLookup.Add(temperatureData[0][i], i);
    5. }
    6.  
    7. 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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. Debug.Log(longLookup[90]);
    As for Dictionary:

    Code (csharp):
    1. C#:
    2. Dictionary<string, string> openWith = new Dictionary<string, string>();
    3. JS:
    4. 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:

    Code (csharp):
    1. C#:
    2. var openWith = new Dictionary<string, string>();
    3. JS:
    4. 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