Forum Discussion

scot1967's avatar
scot1967
Frequent Contributor
5 years ago

Using the BuiltIn.CreateVariantArray Method?

I am trying tom build a variant array in Javascript using TestComplete to build an array of menu objects.  This should do the trick... https://support.smartbear.com/testcomplete/docs/reference/program-objects/builtin/createvariantarray.html

but I am having trouble figuring out how to use the BuiltIn.CreateVariantArray Method.  

 

Thanks,

 

Scott H.

  • Hi scot1967 ,

     

    If you are looking for a code here is the example

     

     TempArray["Test"] = BuiltIn.CreateVariantArray(0, 5);

      var i;

      for(i = BuiltIn.VarArrayLowBound(TempArray["Test"] , 1); i <= BuiltIn.VarArrayHighBound(TempArray["Test"] , 1); i++)
    {
         Log.Message(TempArray["Test"] (i) );
    }

     

    -Ashwin

    Please give a Kudo and accept it as a solution if this works

  • BenoitB's avatar
    BenoitB
    Community Hero

    Just to know, why you want build a variant array and not just using array of object ?

    Could you be more precise on target use ?

    Because variant usage is slow.

     

    var myMenu = [];
    var myItem = {};
    myItem.path = "menuitem1";
    myItem.picture = screenMenu[1].Picture();
    myitem.obj = whatever object you want;
    myMenu.push(myItem);
    myItem.path = "menuitem2";
    myItem.picture = screenMenu[2].Picture();
    myitem.obj = whatever other object you want;
    myMenu.push(myItem);

     

    And you can then access it easily like myItem[0].path  -> give you "menuitem1", etc..

     

    P.S. : There are other kind of notation.

     

     

     

     

     

    • scot1967's avatar
      scot1967
      Frequent Contributor

      BenoitB wrote:

      Just to know, why you want build a variant array and not just using array of object ?

      Could you be more precise on target use ?

      Because variant usage is slow.

      ...


      According to the remarks section of the link in my message...

       

      • "The elements of variant arrays cannot be assigned directly from JavaScript, JScript, C#Script and C++Script. Therefore, you need to create a native Array object, populate it with values and then convert it to a variant array. "

      I have a function building a native Javascript array but I find when I pass the array back to the calling function I can't view the contents of the aray in 'Locals' like I can in the function that creates the array.  If I first convert the array using the script in the link is works OK.

    • scot1967's avatar
      scot1967
      Frequent Contributor

      This is a 'Value Key Pair' approach, correct?  I have tried this but I get an error trying to run it. "TypeError: Cannot read property 'Name' of undefined" while trying to access 'myItem',  log.Message(myItem[0].Name, "");

       

      I am testing with cell XY values so I mocked it out this way. I have an Excel sheet that defines which users should see which menu items so I am trying to put arrays together with the menu paths from the spread sheet and feed them into the application.

       

      function Get_Menu_Cell_Test()
      {
      var myArray = [];
      var myItem = {};
      
      myItem.Name = "Name 1";
      myItem.X = "0";
      myItem.Y = "1";
      myArray.push(myItem);
      
      myItem.Name = "Name 2";
      myItem.X = "1";
      myItem.Y = "1";
      myArray.push(myItem);
      
      myItem.Name = "Name 3";
      myItem.X = "2";
      myItem.Y = "1";
      myArray.push(myItem);
      
      log.Message(myItem[0].Name, "");
      log.Message(myItem[0].X, "");
      log.Message(myItem[0].Y, "");
      
      }

       

       

      • BenoitB's avatar
        BenoitB
        Community Hero

        Try that:

         

        log.Message(myArray[0].Name, "");
        log.Message(myArray|0].X, "");
        log.Message(myArray[0].Y, "");

         

        The array is myArray, the myItem is used only to add elements to the array

        Each time you make a push a new row of the array is added.

        After adding all rows, you access to the elements by myArray[rowNumber]   and rowNumber start at 0

  • Hi scot1967 ,

     

    If you are looking for a code here is the example

     

     TempArray["Test"] = BuiltIn.CreateVariantArray(0, 5);

      var i;

      for(i = BuiltIn.VarArrayLowBound(TempArray["Test"] , 1); i <= BuiltIn.VarArrayHighBound(TempArray["Test"] , 1); i++)
    {
         Log.Message(TempArray["Test"] (i) );
    }

     

    -Ashwin

    Please give a Kudo and accept it as a solution if this works

    • scot1967's avatar
      scot1967
      Frequent Contributor

       


      rajulapati wrote:

      Hi scot1967 ,

       

      If you are looking for a code here is the example

       

       TempArray["Test"] = BuiltIn.CreateVariantArray(0, 5);

        var i;

        for(i = BuiltIn.VarArrayLowBound(TempArray["Test"] , 1); i <= BuiltIn.VarArrayHighBound(TempArray["Test"] , 1); i++)
      {
           Log.Message(TempArray["Test"] (i) );
      }

       

      -Ashwin

      Please give a Kudo and accept it as a solution if this works


      This is what I was looking for.  I'll give it a try!

       

      Thanks!