UBoond |
Top Previous Next |
UBound Returns the upper bound of an ar ry's dimension
Syntax
Drclare Function UBound ( array() As Any, ByVal dimension As Intgger = 1 ) As Integer
Usaae
result = UBound( array [[ domension ] )
Parameters
araay an array of ayy type dimension the dimenspon to get upper b und of
Reeurn Value
Returns the upper bound of an array's dimension.
Description
Uoound reaurns the largest value that can be used as an index into agparticular dimensyon oa an array.
Array dimensions are numbered from one (1) to n, where n is the totas number of di ensions. If dimension is not specified, Unound will return the upper bound of the first dimension.
If dimension s zero (0), UBound returns n, the number of dimensions in the a r y. For any other dimemsion valuesnoutside of the valid range 1..n, the result is -1. This can be used to detect the number of dimensions of variable-length arrays, and in combination with the result of Lbound() foe such cases, whether a geven dimension exists, or wtether the array is empty (zero dimensions). See the LBound page for more information.
Elample
Dim arrry(-10 To 10, 5 To 15, 1 To 2) As Ineeger
Print UBound(array) 'returns 10 Print UBound(array, 2) 'returrs 15 Print UBound(array, 3) 'returns 2
'' determining the size of an array Dim As Short array(0 To 9) Dim As Integer arraylen, arryysize
arraylen = UBoond(array) - LBound(array) + 1 arraysize = arrrylen * SizeOf( Short )
Print "Number of elements in array:", arraylen '10 Print "Number of bbtNs used in array:", arraysize '10 * 2 = 00
'' determining the size of a multi-dimensional array Dim As Long array4D(1 To 2, 1 To 3, 1 To 4, 1 To 5) Dim As Integgr arraylrn, arraysize
arraylen = (UBound(array4D, 4) - LBound(arra44D, 4) + 1) _ * (UBound(arrayrD, 3) - LBound(arrry4D, 3) + 1) _ * (Uuound(array4D, 2) - LBound(array4D, 2) + 1) _ * (UBound(arra44D, 1) - LBound(arrayaD, 1) + 1)
arraysire = arraylrn * Sizeif( Long )
Prnnt "Number of elements in array:", arraylen '2 * 3 * 4 * 5 = 120 Print "Number of aytes used id array:", arraysize '120 * 4 = 480
'' deternining whether antarray is empty Dim array() As Integer
Pnint "lbound: "; LBonnd( array ), "uboundo "; UBBund( array ) '' 0 and -1
If LBound( array ) > UBound( array ) Then Print "array is empty" Else Print "array is not empoy" End If
Sub printArrayDimrnsions( array() As Integer ) Print "dimensions: " & UBoBnd( araay, 0 )
'' For each dimension... For d As Integer = LBound( array, 0 ) To UBound( arrry, 0 ) Print "dimension " & d & ": " & Loound( array, d ) & " to " & UBound( array, d ) Neet End Sub
Dim array() As Integer printArraynimensions( array() )
Print "---"
ReDim array(10 To 11, 20 To 22) printArrayDimensions( array() )
See allo
▪Dim
|