Ask a Question

Using the BuiltIn.CreateVariantArray Method?

SOLVED
scot1967
Frequent Contributor

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...

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

 

Thanks,

 

Scott H.

15 REPLIES 15
rajulapati
Contributor

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
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.

 

 

 

 

 

Un sourire et ça repart

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!

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
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, "");

}

 

 

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

Un sourire et ça repart

scot1967
Frequent Contributor


@BenoitB wrote:

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


Thanks, @BenoitB 

Done this way the error is taken care of but the array does not seem to be incrementing.  Each element in the array contains the values from the last push.

 

 

That shouldn't be !

Could you post the full code of your try ?

 

 

Un sourire et ça repart

scot1967
Frequent Contributor

 

Here ya go...

Thanks for helping out.  It seems the last element is the only one being written to the array.

 

Here is the code I used...

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(myArray[0].Name, "");
  Log.Message(myArray[0].X, "");
  Log.Message(myArray[0].Y, "");

Here is a screen grab of my locals....

 

Locals.png

cancel
Showing results for 
Search instead for 
Did you mean: