
How to Return 1 UDT element from Function
I'm having a little trouble with this function:
Public Function GetPriorPrices(ByVal dDate As Date) As DATAARRAY
Dim n As Long
For n = 1 To UBound(MarketData) 'start at 1 because we compare
back a bar
If MarketData(n).dDate = dDate Then
GetNextPrices = MarketData(n - 1)
Exit Function
End If
Next n
End Function
This function is passed a Date, in which it uses this Date to locate
within the MarketData() array of type DATAARRAY the correct data item
and return that data item back to the calling routine.
Now, the UDT DATAARRAY (simplified) is like this:
Type DATAARRAY
dDate as Date
Price as Single
End Type
Say the calling routine does this:
Dim PriceFromDate as DATAARRAY
PriceFromDate = GetPriorPrices(sSomeDate)
So that the calling routine can access this returned data by
viewing...
PriceFromDate.dDate and PriceFromDate.Price
This does not work, however.
Compile error:
Function call on left-hand side of assignment must return Variant or
Object.
So I change the function to return a Variant rather than DATAARRAY
type.
Now I get...
Compile error:
Only user-defined types defined in public object modules can be
coerced to or from a variant or passed to late-bound functions.
What kind of hoops do I need to jump to simply return the one array
item of type DATAARRAY from the function?
Thanks.
Webbiz