cancel
Showing results for 
Search instead for 
Did you mean: 

Using the BuiltIn.CreateVariantArray Method?

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

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

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

View solution in original post

15 REPLIES 15
Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

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

View solution in original post

Highlighted
Community Leader

Re: Using the BuiltIn.CreateVariantArray Method?

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

Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

 


@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!

Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?


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

Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

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

}

 

 

Highlighted
Community Leader

Re: Using the BuiltIn.CreateVariantArray Method?

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

Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?


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

 

 

Highlighted
Community Leader

Re: Using the BuiltIn.CreateVariantArray Method?

That shouldn't be !

Could you post the full code of your try ?

 

 

Un sourire et ça repart

Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

 

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

Tags (1)