Forum Discussion

coffee's avatar
coffee
Contributor
8 years ago
Solved

VarArrayHighBound Method

Hi, I am trying to understand what the method VarArrayHighBound does.

I found the documentation https://support.smartbear.com/viewarticle/86740/?q=VarArrayHighBound  and try this one (in Javascript)

 

function abc () {

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
x = BuiltIn.VarArrayHighBound (person, 1);
Log.Message(x);

}

TC returns 

Error
VarArrayHighBound
Invalid argument

I don't quite understand how this method work. 

Also, what is Dim?  If I have 1 dimension array with 4 elements like my "var person" , then Dim should be "1" ?   If I have 2 dimension array , then Dim should be "2" ?   Thanks. 

 

Dim
The index of the array's dimension whose high bound you want to get. The leftmost dimension has index 1, the second dimension index is 2, etc.

 


  • coffee wrote:

    I saw somewhere on this website this piece of codes

      ...
      // Call the function
      arr = TestedPage.QuerySelectorAll(CSSSelector);
      // Check the result
      cnt = BuiltIn.VarArrayHighBound(arr,1);
      ...

    I just want to understand what does cnt = BuiltIn.VarArrayHighBound(arr,1); do actually.  :smileyhappy:   Thank You.


    That example is not quite correct, we'll fix it. Thanks for the notice! The correct JavaScript and JScript code would be:

    // JavaScript
    arr = TestedPage.QuerySelectorAll(CSSSelector);
    if (arr.length > 0)
      // found
    else
      // not found
    // JScript
    arr = TestedPage.QuerySelectorAll(CSSSelector);
    arr = arr.toArray(); // convert the result to a native JScript array
    if (arr.length > 0) // found else // not found

    To answer the question about VarArrayHighBound, it is used in VBScript and DelphiScript code to determine the high bound of an array. It's an analog of VBScript's UBound function. VarArrayLowBound and VarArrayHighBound are used to determine the range of array indexes when you need to iterate through an array in VBScript or DelphiScript, because arrays in these languages do not necessarily use zero-based indexing.

     

    In JavaScript and JScript, you simply use arr.length.

     

    Hope this helps!

7 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    VarArrayHighBound is for VBScript and DelphiScript arrays. It won't work with native JScript/JavaScript arrays and objects (note that "person" in your code is an object, not an array).

     

    What exactly are you trying to accomplish? Maybe we can suggest a better solution for your actual problem.

    • coffee's avatar
      coffee
      Contributor

      Thanks all. 

       

      I saw somewhere on this website this piece of codes

       

       

      function FindByCSSSelector()
      {
      var TestedPage, CSSSelector, arr, res, cnt;

        Browsers.Item(btChrome).Run("http://smartbear.com/");
        TestedPage = Sys.Browser('*').Page("http://smartbear.com/");

        CSSSelector = "a.nav-cat-main";
        
        // Call the function
        arr = TestedPage.QuerySelectorAll(CSSSelector);
        // Check the result
        cnt = BuiltIn.VarArrayHighBound(arr,1);
          
        if (cnt > -1)
          {
            // Matching elements were found
            Log.Message("Found " + intToStr(cnt+1)+ " elements matching the CSS selector: " + CSSSelector);
            // Convert the array to the JScript-compatible format
            for (let i=0;i<=cnt;i++)
              {
                // Output the element's content
                Log.Message(arr[i].contentText);
              }
          }
        else
          {
            // No matching elements were found
            Log.Warning("Did not find any elements matching the CSS selector: " + CSSSelector);
          }
      }

      I just want to understand what does cnt = BuiltIn.VarArrayHighBound(arr,1); do actually.  :smileyhappy:   Thank You.