I have a function that takes an array of strings and map each string to a Date
instance. The function boils down to the following code.
Private Function ParseDates(dates() As String) As Date()
Dim res() As Date
Dim i As Integer
If UBound(dates) >= 0 Then
ReDim res(UBound(dates)) As Date
End If
For i = LBound(dates) To UBound(dates)
res(i) = #01/01/2000#
Next i
ParseDates = res
End Function
The function works just fine as long as the argument dates
is nonempty. When dates
is empty res
is not given a dimension. As a result, the returned value is not enumerable causing to users of this function to crash if the result is enumerated in a loop.
parsedDates = ParseDates(input)
For i = 1 To UBound(parsedDates) ' Suscription out of range
...
How do I instantiate and return an empty array, when dates is empty?
If you call Split("",".")
you receive an object with type String(0 to -1)
. I need my function to return a object of type Date(0 to -1)
as Date()
is not an actual array.
I have tried with ReDim res(-1)
this causes an Subscript out of range
error.