Using the BuiltIn.CreateVariantArray Method?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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, ""); }
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That shouldn't be !
Could you post the full code of your try ?
Un sourire et ça repart
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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....
