### Revision history for KeyPgUbound

#### Revision 

Last edited on 2019-12-06 12:30:11 by fxm [corrected the erroneous comment in the fourth example]
print "lbound: "; lbound( array ), "ubound: "; ubound( array ) '' 0 and -1
##### Deletions:
print "lbound: "; lbound( array ), "ubound: "; ubound( array ) '' 1 and 0

#### Revision 

Edited on 2016-02-10 16:09:53 by DkLwikki [Update link format]
[[KeyPgDeclare|declare]] [[KeyPgFunction|function]] **Ubound** ( array() [[KeyPgAs|as]] [[KeyPgAny|any]], [[KeyPgByval|byval]] //dimension// [[KeyPgAs|as]] [[KeyPgInteger|integer]] = 1 ) [[KeyPgAs|as]] [[KeyPgInteger|integer]]
If ##//dimension//## is zero (0), ##**Ubound**## returns ##//n//##, the number of dimensions in the array. For any other ##//dimension//## values outside 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()## for such cases, whether a given dimension exists, or whether the array is empty (zero dimensions). See the ##[[KeyPgLbound|Lbound]]## page for more information.
* (UBound(array4D, 3) - LBound(array4D, 3) + 1) _
* (UBound(array4D, 2) - LBound(array4D, 2) + 1) _
* (UBound(array4D, 1) - LBound(array4D, 1) + 1)
- ##[[KeyPgLbound|Lbound]]##
- ##[[KeyPgStatic|Static]]##
- ##[[KeyPgDim|Dim]]##
- ##[[KeyPgRedim|Redim]]##
- ##[[KeyPgSizeof|Sizeof]]##
##### Deletions:
[[KeyPgDeclare declare]] [[KeyPgFunction function]] **Ubound** ( array() [[KeyPgAs as]] [[KeyPgAny any]], [[KeyPgByval byval]] //dimension// [[KeyPgAs as]] [[KeyPgInteger integer]] = 1 ) [[KeyPgAs as]] [[KeyPgInteger integer]]
If ##//dimension//## is zero (0), ##**Ubound**## returns ##//n//##, the number of dimensions in the array. For any other ##//dimension//## values outside 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()## for such cases, whether a given dimension exists, or whether the array is empty (zero dimensions). See the ##[[KeyPgLbound Lbound]]## page for more information.
* (UBound(array4D, 3) - LBound(array4D, 3) + 1) _
* (UBound(array4D, 2) - LBound(array4D, 2) + 1) _
* (UBound(array4D, 1) - LBound(array4D, 1) + 1)
- ##[[KeyPgLbound Lbound]]##
- ##[[KeyPgStatic Static]]##
- ##[[KeyPgDim Dim]]##
- ##[[KeyPgRedim Redim]]##
- ##[[KeyPgSizeof Sizeof]]##

#### Revision 

Edited on 2013-05-27 15:47:09 by DkLwikki [Update for 0/-1 instead of 1/0]
If ##//dimension//## is zero (0), ##**Ubound**## returns ##//n//##, the number of dimensions in the array. For any other ##//dimension//## values outside 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()## for such cases, whether a given dimension exists, or whether the array is empty (zero dimensions). See the ##[[KeyPgLbound Lbound]]## page for more information.
##### Deletions:
If ##//dimension//## is zero (0), the result is the number of dimensions in the array. For any other cases of ##//dimension//## being outside of the valid range ##1##..##//n//##, the result is ##0##. Combined with the result of Lbound() for such cases, this can be used to detect the number of dimensions of variable-length arrays, for example whether a given dimension exists, or whether the array is empty (zero dimensions).

#### Revision 

Edited on 2012-12-26 10:01:18 by DkLwikki [update: invalid dimension is now ok]

Array dimensions are numbered from one (##1##) to ##//n//##, where ##//n//## is the total number of dimensions. If ##//dimension//## is not specified, ##**Ubound**## will return the upper bound of the first dimension.

If ##//dimension//## is zero (0), the result is the number of dimensions in the array. For any other cases of ##//dimension//## being outside of the valid range ##1##..##//n//##, the result is ##0##. Combined with the result of Lbound() for such cases, this can be used to detect the number of dimensions of variable-length arrays, for example whether a given dimension exists, or whether the array is empty (zero dimensions).
{{fbdoc item="filename" value="examples/manual/array/ubound4.bas"}}%%(freebasic)
'' determining whether an array is empty
dim array() as integer
print "lbound: "; lbound( array ), "ubound: "; ubound( array ) '' 1 and 0
if lbound( array ) > ubound( array ) then
print "array is empty"
else
print "array is not empty"
end if
{{fbdoc item="filename" value="examples/manual/array/ubound5.bas"}}%%(freebasic)
sub printArrayDimensions( array() as integer )
print "dimensions: " & ubound( array, 0 )
'' For each dimension...
for d as integer = lbound( array, 0 ) to ubound( array, 0 )
print "dimension " & d & ": " & lbound( array, d ) & " to " & ubound( array, d )
next
end sub
dim array() as integer
printArrayDimensions( array() )
print "---"
redim array(10 to 11, 20 to 22)
printArrayDimensions( array() )
##### Deletions:
Array dimensions are numbered from one (##1##) to ##//n//##, where ##//n//## is the total number of dimensions. If ##//dimension//## is not specified, ##**Ubound**## will return the upper bound of the first dimension. If ##//dimension//## is less than one (##1##) or greater than the total number of dimensions (##//n//##) in the array, the result is undefined.

#### Revision 

Edited on 2008-09-04 19:00:23 by CountingPine [Add examples for determining the length and size of an array]
arraylen = UBound(array) - LBound(array) + 1
arraylen = (UBound(array4D, 4) - LBound(array4D, 4) + 1) _
* (UBound(array4D, 3) - LBound(array4D, 3) + 1) _
* (UBound(array4D, 2) - LBound(array4D, 2) + 1) _
* (UBound(array4D, 1) - LBound(array4D, 1) + 1)
##### Deletions:
arraylen = UBound(array) -LBound(array) + 1
arraylen = (UBound(array4D, 4) -LBound(array4D, 4) + 1) _
* (UBound(array4D, 3) -LBound(array4D, 3) + 1) _
* (UBound(array4D, 2) -LBound(array4D, 2) + 1) _
* (UBound(array4D, 1) -LBound(array4D, 1) + 1)

#### Revision 

Edited on 2008-09-04 18:58:58 by CountingPine [Add examples for determining the length and size of an array]
Array dimensions are numbered from one (##1##) to ##//n//##, where ##//n//## is the total number of dimensions. If ##//dimension//## is not specified, ##**Ubound**## will return the upper bound of the first dimension. If ##//dimension//## is less than one (##1##) or greater than the total number of dimensions (##//n//##) in the array, the result is undefined.
{{fbdoc item="filename" value="examples/manual/array/ubound2.bas"}}%%(freebasic)
'' determining the size of an array
Dim As Short array(0 To 9)
Dim As Integer arraylen, arraysize
arraylen = UBound(array) -LBound(array) + 1
arraysize = arraylen * SizeOf( Short )
Print "Number of elements in array:", arraylen '10
Print "Number of bytes used in array:", arraysize '10 * 2 = 20
{{fbdoc item="filename" value="examples/manual/array/ubound3.bas"}}%%(freebasic)
'' 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 Integer arraylen, arraysize
arraylen = (UBound(array4D, 4) -LBound(array4D, 4) + 1) _
* (UBound(array4D, 3) -LBound(array4D, 3) + 1) _
* (UBound(array4D, 2) -LBound(array4D, 2) + 1) _
* (UBound(array4D, 1) -LBound(array4D, 1) + 1)
arraysize = arraylen * SizeOf( Long )
Print "Number of elements in array:", arraylen '2 * 3 * 4 * 5 = 120
Print "Number of bytes used in array:", arraysize '120 * 4 = 480
- ##[[KeyPgSizeof Sizeof]]##
##### Deletions:
Array dimensions are numbered from one (1) to //n//, where //n// is the total number of dimensions. If ##//dimension//## is not specified, ##**Ubound**## will return the upper bound of the first dimension. If ##//dimension//## is less than one (1) or greater than the total number of dimensions (//n//) in the array, the result is undefined.

#### Revision 

The oldest known version of this page was created on 2007-07-22 20:49:31 by JeffMarshall [Add examples for determining the length and size of an array]