Forum Discussion

Tom_Welch's avatar
Tom_Welch
New Contributor
12 years ago

aqObject.GetVarType does not work correctly with arrays?

I have read the help page at this address: http://support.smartbear.com/viewarticle/57577/



It says that I can use the GetVarType method on aqObject to return a number indicating what variable type I am using. The table at the top of the page says:













varArray

2000h

8192

Array. Type of array elements is specified by the lower bits. For example, 2003h is an array of integers.



However, in the code example given it says:



  Arr       = new Array(1,2,3) 

  TypeID    = GetVarType(Arr);

  TypeDescr = GetTypeName(TypeID);

  Log.Message("Type: " + TypeID + " - " + TypeDescr);

  // Prints "Type: 9 - Automation object of IDispatch interface



But this is clearly not the desired return value. I need to distinguish whether or

not I have been passed an array froma basic IDispatch implementing object. I

have tried the following code, but to no avail:





function TestGetType()


{


  var arr = new Array("s", "c");


  var type = GetVarType(arr);


  Log.Message(type);//=> 9 (Automation object of IDispatch interface - wrong)


  


  arr = [1, 2, 3];


  type = GetVarType(arr);


  Log.Message(type);//=> 9 (Automation object of IDispatch interface - wrong)


  


  var str = "Hi";


  type = GetVarType(str);


  Log.Message(type);//=> 8 (OleStr - Correct!)


  


  var num = 1234.12


  type = GetVarType(num);


  Log.Message(type);//=> 5 (Double - Correct!)


}



How do I actually check to make sure that a variable is in fact an array? 

1 Reply

  • Tom_Welch's avatar
    Tom_Welch
    New Contributor

    Found a work around, but why doesn't GetVarType work as expected?



    /*Checks if an object is an array. 


     


    @Return - a boolean value indicating whether the object is an array


     


    Param obj - the object to check.*/


    function IsArray(obj)


    {


      try


      {


        var tst = obj[0];


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


        {


          tst = obj;


        }


        if(Object.prototype.toString.call(obj) == '[object Array]')


        {


          return true;


        }


        else


        {


          return false;


        }


      }


      catch(err)


      {


        return false;


      }


    }