Forum Discussion

guoguo's avatar
guoguo
Contributor
9 years ago
Solved

Question about value type in the dotNET.System_Collections.Hashtable()?

Hi all,

 

In our test, we need a hashtable to store the test values. Key is string, value is array. The fDict.Add doesn't work. Does anyone know, how to use dotNET.System_Collections.Hashtable() for this case? 

 

var fDict =dotNET.System_Collections.Hashtable().zctor();

var val1 = ["100", "160", "200", "250", "300"];

fDict.Add("key1", val1);

 

Best Regards,

Jie

 

  • You need to use a .NET array instead of a native JScript array.

     

    function Test()
    {
      var fDict = dotNET.System_Collections.Hashtable().zctor();
      var arr1 = ["100", "160", "200", "250", "300"];
      fDict.Add("key1", JSStringArray2NETArray(arr1));
     
      Log.Message(fDict.Item("key1").Length); // 5
      Log.Message(fDict.Item("key1").Get(1).OleValue);  // 160
    }
    
    
    // Convert a JScript string array into a .NET string array.
    // JScript: ["a", "b", "c"]  => C#: string[] {"a", "b", "c"}
    function JSStringArray2NETArray(JSArray)
    {
      var separator = " ";
      var separator2 = dotNET.System.String.Copy(separator).ToCharArray();
    
      var str = JSArray.join(separator);
      var arr = dotNET.System.String.Copy(str).Split(separator2);
      return arr;
    }

2 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You need to use a .NET array instead of a native JScript array.

     

    function Test()
    {
      var fDict = dotNET.System_Collections.Hashtable().zctor();
      var arr1 = ["100", "160", "200", "250", "300"];
      fDict.Add("key1", JSStringArray2NETArray(arr1));
     
      Log.Message(fDict.Item("key1").Length); // 5
      Log.Message(fDict.Item("key1").Get(1).OleValue);  // 160
    }
    
    
    // Convert a JScript string array into a .NET string array.
    // JScript: ["a", "b", "c"]  => C#: string[] {"a", "b", "c"}
    function JSStringArray2NETArray(JSArray)
    {
      var separator = " ";
      var separator2 = dotNET.System.String.Copy(separator).ToCharArray();
    
      var str = JSArray.join(separator);
      var arr = dotNET.System.String.Copy(str).Split(separator2);
      return arr;
    }
  • sanjay0288's avatar
    sanjay0288
    Frequent Contributor

    Hi,

       I am afraid it is not possible to use a Single key with the hashtable. You need to create a multimap hash table or pass it as a structure .

     

    What is the exact requirement? You could create multiple keys dynamically and map to the values and work with it.

     

    The corresponding VB script code is.

     

    Set x= dotNet.System_Collections.Hashtable.zctor

    val1=Array("100","160","200","250","300")

     

    for i=0 to UBound(val1)

       Call x.Add("Key" & (i),val1(i))

     

    Next