How to Return 1 UDT element from Function 
Author Message
 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



Sat, 12 May 2012 08:54:59 GMT  
 How to Return 1 UDT element from Function
Typo...

Public Function GetPriorPrices(ByVal dDate As Date) As DATAARRAY

Quote:
>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

GetNextPrices = MarketData(n-1) is actually

GetPriorPrices = MarketData(n-1)

Oversight.

Webbiz



Sat, 12 May 2012 09:00:30 GMT  
 How to Return 1 UDT element from Function
...

Quote:
> What kind of hoops do I need to jump to simply return the one array
> item of type DATAARRAY from the function?

...

Turn the function into a subroutine is the logical choice--

--



Sat, 12 May 2012 09:00:55 GMT  
 How to Return 1 UDT element from Function
That typo oversight appears to be why my function was not working to
begin with.

Public Function GetPriorPrices(ByVal dDate As Date) As DATAARRAY

Quote:
>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

Changing GetNextPrices to what was intended, GetPriorPrices, did the
trick.

I knew that thing should have worked. Dumb dumb dumb.

Thanks.

Case closed.

Webbiz



Sat, 12 May 2012 09:03:23 GMT  
 How to Return 1 UDT element from Function
I would change the function name that returns DATAARRAY to
GetPriorPricesInfo(), and use GetPriorPrices() to return just the price.
Example:

Public Function GetPriorPricesInfo(ByVal dDate As Date) As DATAARRAY
    ' Code here
End Function

Public Function GetPriorPrices(ByVal dDate As Date) As Single
    Dim udtDataArray As DATAARRAY

    udtDataArray = GetPriorPricesInfo(udtDataArray)
    If udtDataArray = 0 Then
        GetPriorPrices = -1 ' Not found
    Else
        GetPriorPrices = udtDataArray.Price
    End If
End Function

I would do the same to return the date, but call the function
GetPriorPricesDate().

Also, using Single is not a good idea for financial data. Try Double or
Currency.



Sat, 12 May 2012 09:10:12 GMT  
 How to Return 1 UDT element from Function

Quote:


> ...

>> What kind of hoops do I need to jump to simply return the one array
>> item of type DATAARRAY from the function?
> ...

> Turn the function into a subroutine is the logical choice--

Ignore my babbling...I won't even _try_ to explain what I (mis)read the
code to be trying to do... :(

--



Sat, 12 May 2012 10:56:36 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to return UDT's in a collection from a function

2. Returning array of type UDT

3. HOWTO return different udt based on parameter(s) passed

4. Returning an Array of UDT

5. Returning an UDT by reference

6. returning array of UDT

7. UDT returned from method error?

8. Passing a byte array returned from an API into a UDT

9. Returning an Array of UDT's

10. Return multiple elements of a delimited string

11. how to return elements of the current contact

12. Returning Array Elements?

 

 
Powered by phpBB® Forum Software