Forum Discussion

tinauser's avatar
tinauser
Frequent Contributor
14 years ago

save object in table

Hi,

here is the problem: let say I have an object:



var myArr=[];



I can populate it in this way:



for (var i=0;i<10;i++)

    myArr = i;



now, let say I have created a project variable of the Table type, myTable, with one column, myCol, and one row;

in the row I save the object myArr=[]



now let say I call back myArr from the table and try to populate the object as I did before:



myArr = (Project.Variables.VariableByName("myTable")).Item("myCol",0);

myArr[0]=0;//this line will raise an error:

"Object doesn't support this property or method"



What happened to the old object??

What am I doing wrong??



Thanks








8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Here is the code I wrote to simulate the same thing in JScript





    function blah()

    {

        var MyArray = [];

        for (var i = 0; i <10;i++)

        {

            MyArray=i;    

        }

        Project.Variables.MyTable.Item("MyCol", 0) = MyArray;

        MyArray = Project.Variables.VariableByName("MyTable").Item("MyCol", 0);

        MyArray[0] = "yada"

        Log.Message(MyArray[0]);

        Log.Message(MyArray[1]);

    }





    Worked just fine.  My test log has two entries, the first with the word "yada", the second with the number 1.



    What are you doing differently than me?
  • tinauser's avatar
    tinauser
    Frequent Contributor
    Instead of myArray[0]="yada"

    Try myArray[10]="blabla";



    if myArray was an object prepared

    for (...<10)

       myArray=i;



    this line would be correct; if you fetch the object as an item of the table, is not.

    It seems that object retrieved from the table as item are not real java object.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Okay... I THINK I got it.



    It has to do with what sorts of arrays TestComplete works with versus the native JScript array object type.  You kind of have to switch things back and forth.  From the help topic http://smartbear.com/support/viewarticle/13265/#JS, I adapted the code as such... this works...Essentially, I create a native JScript array... convert it to a VBArray, then convert it back to a JScript array



    function ConvertJScriptArray(JScriptArray)


    {


      // Uses the Dictionary object to convert a JScript array


      var objDict = Sys.OleObject("Scripting.Dictionary");


      objDict.RemoveAll();


      for (var i in JScriptArray)


        objDict.Add(i, JScriptArray);


      return objDict.Items();









    function blah()


    {


        var MyArray = new Array();


        for (var i = 0; i <10;i++)


        {


            MyArray=i;    


        }


        MyArray = ConvertJScriptArray(MyArray)


        Project.Variables.MyTable.Item("MyCol", 0) = MyArray;


        var foo = new VBArray(Project.Variables.VariableByName("MyTable").Item("MyCol", 0));


        foo = foo.toArray();    


        foo[10] = "yada"


        Log.Message(foo[10]);


        Log.Message(foo[1]);


    }








  • tinauser's avatar
    tinauser
    Frequent Contributor
    Well,yes, this will work...I think it would be enough to use a function like

    function copy(o)

    {

      var c=[];

      for(var i=0;i<o.length;i++)  

      {

        c=o;  

      }

      return c;

    }

    And pass the item from the table.



        Project.Variables.MyTable.Item("MyCol", 0) = MyArray;


        MyArray = copy( Project.Variables.VariableByName("MyTable").Item("MyCol", 0));





  • tinauser's avatar
    tinauser
    Frequent Contributor
    By the way,

    is that for the same reason that if I have



    var arr[];

    and then I do:



    arr["blabla"] = [1,2,3];

    arr["bla bla"] = [1,2,3];



    In the local variable panel the variable arr will not display arr["bla bla"] (because it is an jscript extendo, and TC runs natively as VB)?



    Thanks a lot for the clarification.



    Lorenzo
  • Hi Lorenzo,



    The array issue you originally described is indeed caused by specifics of JScript's array format, which is not Variant-compatible. The dev team is aware of the issue and will try to address it in a future release. Until then, the workaround you and Robert came up with -- create the array in a script variable, modify it as necessary and once it's ready copy it to original project variable -- is the best option.







    var arr[];

    and then I do:



    arr["blabla"] = [1,2,3];

    arr["bla bla"] = [1,2,3];



    In the local variable panel the variable arr will not display arr["bla bla"]



    I've forwarded this to the dev team for investigation. Thanks for bringing this up!
  • Hi Lorenzo,



    Just a quick update: the issue with the Locals panel not displaying the contents of arr["bla bla"] is fixed in TestComplete 9.
  • tinauser's avatar
    tinauser
    Frequent Contributor
    Thnaks :)

    Although now all my testing framework uses workaround :)