Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lbound and Ubound Functions

Similar presentations


Presentation on theme: "Lbound and Ubound Functions"— Presentation transcript:

1 Lbound and Ubound Functions
Passing Arrays between Procedures and Returning an Array from a Function Lbound and Ubound Functions

2 Lbound and Ubound Functions
The Lbound and Ubound functions enable you to test the upper and lower boundaries of an array when they are unknown to you. Lbound returns the smallest available array subscript. Ubound returns the largest available array subscript. LBound(arrayname[, dimension]) UBound(arrayname[, dimension]) arrayname Required. Name of the array dimension Optional; Variant (Long). Whole number indicating which dimension's upper bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.

3 UBound and LBound Example
Dim Upper Dim Lower Dim MyArray(1 To 10, 5 To 15, 10 To 20) Dim AnyArray(10) Upper = UBound(MyArray, 1) Upper = UBound(MyArray, 3) Lower = LBound(AnyArray) Lower = LBound(MyArray, 3)

4 Returning an Array from a Function
Private Sub Form_Load() Dim b As Integer Dim i As Integer Dim ReturnArray() As Integer ReturnArray() = ArrayFunction(b) For i = 0 To Ubound(ReturnArray) picOutput.Print ReturnArray(i) Next End Sub Public Function ArrayFunction(b As Integer) As Integer() Dim x(2) As Integer x(0) = b x(1) = b + 200 x(2) = b + b ArrayFunction = x End Function This example also shows: how to assign one array variable to another an example of the Ubound function

5 Passing Arrays between Procedures
An entire local array can be passed to another procedure. The name of the array, followed by an empty set of parentheses, must appear as an argument in the calling statement, and an array variable name of the same type must appear as a corresponding parameter in the procedure definition.

6 Private Sub cmdDisplay_Click()
Dim score(1 To 10) As Integer Call FillArray(score()) picAverage.Cls picAverage.Print "The average score is"; Sum(score()) / 10 End Sub Private Sub FillArray(s() As Integer) Dim i As Integer For i = 1 to 10 s(i) = 90 + i Next Private Function Sum(s() As Integer) As Integer Dim total As Integer, index As Integer 'Add up scores total = 0 For index = 1 To 10 total = total + s(index) Next index Sum = total End Function


Download ppt "Lbound and Ubound Functions"

Similar presentations


Ads by Google