2

I was looking for some help regarding writing a function for returning a row of a multidimensional array in VBA. Cpearson has a very extensive article on array functions in VBA, with a "library" of functions with a lot of nice features. This article is cited in lots of VBA array questions on StackOverflow. However, I notice that cpearson consequently make use of boolean functions.

Example: the GetRow function given below is boolean function, though I think that the function should return an one-dimensional array from the given row number, something like

Function RtrnArrayRow(SrcArr() As Variant, RowNumber As Integer) As Variant

where RtrnArrayRow is a one-dimensional array.

Question: What is best-practice for applying array functions, boolean or non-boolean, and how to best make proper use of the boolean function below?

All help is appreactiated!

Function GetRow(Arr As Variant, ResultArr As Variant, RowNumber As Long) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetRow
' This populates ResultArr with a one-dimensional array that is the
' specified row of Arr. The existing contents of ResultArr are
' destroyed. ResultArr must be a dynamic array.
' Returns True or False indicating success.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ColNdx As Long
''''''''''''''''''''''''''''''
' Ensure Arr is an array.
''''''''''''''''''''''''''''''
If IsArray(Arr) = False Then
    GetRow = False
    Exit Function
End If

''''''''''''''''''''''''''''''''''
' Ensure Arr is a two-dimensional
' array.
''''''''''''''''''''''''''''''''''
If NumberOfArrayDimensions(Arr) <> 2 Then
    GetRow = False
    Exit Function
End If

''''''''''''''''''''''''''''''''''
' Ensure ResultArr is a dynamic
' array.
''''''''''''''''''''''''''''''''''
If IsArrayDynamic(ResultArr) = False Then
    GetRow = False
    Exit Function
End If

''''''''''''''''''''''''''''''''''''
' Ensure ColumnNumber is less than
' or equal to the number of columns.
''''''''''''''''''''''''''''''''''''
If UBound(Arr, 1) < RowNumber Then
    GetRow = False
    Exit Function
End If
If LBound(Arr, 1) > RowNumber Then
    GetRow = False
    Exit Function
End If

Erase ResultArr
ReDim ResultArr(LBound(Arr, 2) To UBound(Arr, 2))
For ColNdx = LBound(ResultArr) To UBound(ResultArr)
    ResultArr(ColNdx) = Arr(RowNumber, ColNdx)
Next ColNdx

GetRow = True

End Function
Community
  • 1
  • 1
Holene
  • 563
  • 1
  • 7
  • 26

2 Answers2

5

Functions that populate/return arrays are often made to return the array by reference in a parameter because then the return value of the function can be used to see if the array was successfully returned.

Unlike in VB.NET, in VB6/A you cannot easily test if an array exists by doing Is Nothing. Hence this technique of explicitly returning success/failure.

You can use that, or you can adopt one of the ways to test if an array exists, in which case you can return the array instead of boolean.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
0

In line with GSerg's explanation:

The function does a series of validations before processing the "input" array (Arr As Variant) to extract the "output" array (ResultArr As Variant) for the "required" row (RowNumber As Long) if all the validations are successfully passed then it returns the expected outcome as arguments and its value becomes TRUE; if any of the validations fails its value is FALSE, then you know the output is invalid.

Validations:

' Ensures Arr is an array.
' Ensures Arr is a two-dimensional array.
' Ensures ResultArr is a dynamic array.
' Ensures ColumnNumber is less than or equal to the number of columns.

If any of the above fails then

    GetRow = False
    Exit Function

Otherwise generates the "Result" array

Erase ResultArr
ReDim ResultArr(LBound(Arr, 2) To UBound(Arr, 2))
For ColNdx = LBound(ResultArr) To UBound(ResultArr)
    ResultArr(ColNdx) = Arr(RowNumber, ColNdx)
Next ColNdx

and becomes TRUE

GetRow = True

All is explained at the beginning of the function.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetRow
' This populates ResultArr with a one-dimensional array that is the
' specified row of Arr. The existing contents of ResultArr are
' destroyed. ResultArr must be a dynamic array.
' Returns True or False indicating success.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

This Function is designed to behave in this manner in order to pass the information as whether it was able to perform accurately what it's expected to do. This is one of the forms it can be used:

If not GetRow(InputArray, ResultArray , RowNumber ) then Goto ErrorHandler
EEM
  • 6,601
  • 2
  • 18
  • 33
  • 1
    Thank you for your answer. Exactly *what* the function does is quite clear, my question is *why* :-) – Holene Nov 03 '15 at 09:44