Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:emptyarray [2015/09/14] (current)
Line 1: Line 1:
 +====== Testing for Empty Arrays ======
 +> **Developer:​** [[developer:​rhinoscript|RhinoScript]]
 +> **Summary:​** //Discusses how to determine a VBScript array is empty.//
 +
 +=====Question=====
 +I am trying to analyze an array to determine whether or not it is empty. That is, if it has any space to store elements. But, the following test does not seem to work.
 +
 +<code vb>
 + Sub Main()
 +   Dim arr
 +   arr = Array()
 +   If IsArray(arr) Then
 +     ​Rhino.Print("​This should not print"​)
 +   End If    ​
 + End Sub
 +</​code>​
 +
 +=====Answer=====
 +When you execute this statement:
 +
 +<code vb>
 + arr = Array()
 +</​code>​
 +
 +You are declaring an array that has an upper bounds of -1. Because this variable is an array, it will pass the IsArray() test. What the above code needs is an additional test condition to see if the upper bounds of the array is greater than -1.
 +
 +<code vb>
 + Sub Main()
 +   Dim arr
 +   arr = Array()
 +   If IsArray(arr) And UBound(arr) >= 0 Then
 +     ​Rhino.Print("​This should not print"​)
 +   End If    ​
 + End Sub
 +</​code>​
 +
 +Now the code works as expected. But, what if the code looked like this?
 +
 +<code vb>
 + Sub Main()
 +   Dim arr()
 +    If IsArray(arr) And UBound(arr) >= 0 Then
 +     ​Rhino.Print("​This should not print"​)
 +   End If    ​
 + End Sub
 +</​code>​
 +
 +Notice that the above code gives you an "​Script out of range: UBound"​ error. This is because, although the variable is an array, it has not been dimensioned. Thus the call to UBound() fails. So, we need a better test - one that will test for both types of array declarations. Consider the following function:
 +
 +<code vb>
 + ​Function IsArrayDimmed(arr)
 +   ​IsArrayDimmed = False
 +   If IsArray(arr) Then
 +     On Error Resume Next
 +     Dim ub : ub = UBound(arr)
 +     If (Err.Number = 0) And (ub >= 0) Then IsArrayDimmed = True
 +   End If  ​
 + End Function
 +</​code>​
 +
 +Noice how the function above provides error checking. If an error occurs when calling UBound(), it is caught. Thus, the function knows when an array has not been dimensioned. Also, if UBound() returns a value of -1, we know that the array has no space to store elements. We can test this with the following function:
 +
 +
 +<code vb>
 + Sub Main()
 +   Dim arr0
 +   Dim arr1()
 +   Dim arr2(3)
 +   Dim arr3 : arr3 = Array()
 +   Dim arr4 : arr4 = Array(1,​2,​3)
 +   ​Rhino.Print "Arr0 dimmed = " &  IsArrayDimmed(arr0)
 +   ​Rhino.Print "Arr1 dimmed = " &  IsArrayDimmed(arr1)
 +   ​Rhino.Print "Arr2 dimmed = " &  IsArrayDimmed(arr2)
 +   ​Rhino.Print "Arr3 dimmed = " &  IsArrayDimmed(arr3)
 +   ​Rhino.Print "Arr4 dimmed = " &  IsArrayDimmed(arr4)
 + End Sub
 +
 +</​code>​
 +
 +\\
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/emptyarray.txt ยท Last modified: 2015/09/14 (external edit)