Useful Array Functions Properties and Methods

The UBound function returns the upper bound of the indicated dimension of an array. lts syntax is:

UBound(arrayname, [rank])

The rank argument is used in multidimensional arrays, where 1 indicates the first dimension, 2 the second dimension, and so on. lf rank is not specified, the default is 1, which works fine for a one-dimensional array.

The UBound function can be used in the following loop: Dim arrSalesPerDay(6) As lnteger Dim l As lnteger

For l = 0 To UBound(arrSalesPerDay) ' lnstead of For l = 0 To 6

Debug.Write(arrSalesPerDay(l))

Next l

The UBound function permits you to loop through an array without having known the upper bound of the array when you wrote your code.

Note Visual Basic .NET also supports an LBound function, which returns the lowest bound of the array. However, unlike prior versions of Visual Basic, in Visual Basic .NET, the lowest bound of an array is always zero, so the LBound function is not particularly useful.

An array is an object, specifically an Array object. The Array object, like other objects, has properties and methods. Table 8-4 lists and describes seve ral properties and methods you may find useful in your programming.

Table 8-4: Useful Properties and Methods of Array Objects

Property or Method

Description

Clear

Method. Sets a range of elements in an array to default values for their data type, such as zero for numeric values and False for Boolean values.

Copy

Method. Copies a section of one array to another array.

CopyTo

Method. Copies all the elements of a one-dimensional array to another one-dimensional array starting at the specified destination array index.

GetLength

Method. Gets the number of elements in the specified dimension of an array.

GetLowerBound

Method. Gets the lower bound of the specified dimension in an array.

GetUpperBound

Method. Gets the upper bound of the specified dimension in an array.

GetValue

Method. Gets the value of the specified element in an array.

IndexOf

Method. Returns the index of the first occurrence of a value in a one-dimensional array or in a portion of the array.

LastIndexOf

Method. Returns the index of the last occurrence of a value in a one-dimensional array or in a portion of an array.

Length

Property. Total number of elements in all the dimensions of the array.

Rank

Property. Returns number of dimensions in the array.

Reverse

Method. Reverses the order of the elements in a one-dimensional array or in a portion of the array.

SetValue

Method. Sets the specified element in an array to the specified value.

Sort

Method. Sorts the elements in a one-dimensional array, such as sorting strings alphabetically.

Was this article helpful?

0 0

Post a comment