Return an Empty Array from a function
Function should return double(2), but in certain conditions the function can't return a valid array. As a part of my error handling, I want to return an Empty Array (Other options, such as Null or Nothing would be acceptable. An Array of 0s is not acceptable) so that the parent routine can check IsEmpty() and handle the issue correctly. Everything I try, results in IsEmpty() returning False.
Sub Test()
Dim dTest() As Double
dTest = EmptyArrayTest
If IsEmpty(dTest) Then GoTo Err 'Doesn't Work
Exit Sub
Err:
Debug.Print "Err: "
End Sub
Function EmptyArrayTest() As Double()
Dim bSomethingWentWrong As Boolean
Dim dArray(2) As Double
Dim i As Integer
'DoStuff
For i = 0 To 2
dArray(i) = i + 1
Next i
bSomethingWentWrong = True
If bSomethingWentWrong Then GoTo Err
EmptyArrayTest = dArray
Exit Function
Err:
' EmptyArrayTest = Empty 'Type Mismatch
' EmptyArrayTest = Null 'Type Mismatch
' EmptyArrayTest = Nothing 'Invalid use of Object
' Set EmptyArrayTest = Nothing 'Can't assign to Array
' Erase EmptyArrayTest 'Undimensioned array, IsEmpty() returns false
' ReDim EmptyArrayTest(0) '1 element array, IsEmpty() returns false
' ReDim EmptyArrayTest(-1) 'Subscript out of range
' Dim vArray As Variant
' vArray = Empty
' EmptyArrayTest = vArray 'Type mismatch
Debug.Print IsEmpty(EmptyArrayTest) 'Returns False
' Debug.Print UBound(EmptyArrayTest) 'Subscript out of range if array is not dimensioned.
End Function