Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

multiple returns from 1 function...how?

snooglets7snooglets7 Posts: 5Member
The followins my code...I have 4 values to return from the function. As far as I know I have to put them into an array to enable me to call them in a command sub Procedure...I just don't know how! The returns are in Red..the arrStats is the array iw as trying to use for the multiple returns...it's doing my head in!

Private Function getStats(ByVal limDist As Double) As Double

Dim i As Integer 'counter
Dim sumBearg As Double
Dim sumDist As Double
Dim intNum As Integer
Dim avgDist As Double
Dim avgBearg As Double
'Dim maxBearg As Double
'Dim minBearg As Double
Dim maxDist As Double
Dim minDist As Double

'get averages
sumBearg = 0
sumDist = 0
intNum = 0
Try
For i = 0 To intCount - 1
If arrSurvey(i, 1) <= limDist Then
sumDist = sumDist + arrSurvey(i, 1)
sumBearg = sumBearg + arrSurvey(i, 0)
intNum = intNum + 1
End If
Next i
Catch ex As IO.IOException
MessageBox.Show("You need to define a Distance Limit. If you do not want a Distance limit enter 0.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
[color=Red] avgDist = sumDist / intNum
avgBearg = sumBearg / intNum[/color]
'get extreme Distances
Call sortArrayDist()

Dim low, mid, high, targetIndex As Integer
Dim target As Double

Try
targetIndex = -1
low = 0
mid = 0
high = arrSurvey.GetUpperBound(0) - 1 'max index
target = limDist 'to search for

Do While((targetIndex < 0) And (high >= low))
mid = (low + high) / 2
If (arrSurvey(mid, 1) = target) Then 'found it
targetIndex = mid
ElseIf (arrSurvey(mid, 1) > target) Then 'search bottom half
high = mid - 1
Else 'search top half
low = mid + 1
End If
Loop

[color=Red]minDist = arrSurvey(0, 1)
maxDist = arrSurvey(high, 1)[/color]

Catch ex As IO.IOException
MessageBox.Show("You need to define a Distance Limit. If you do not want a Distance limit enter 0.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try

[color=Blue] THIS IS WHAT I CAN"T DO!!!![/color]
[color=Red] Dim arrStats() As Double = {avgDist, avgBearg, minDist, maxDist}[/color]

[color=Red]getStats = arrStats()[/color]
End Function

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    These modifications should work:
    - You forgot to make your function return type as Array by adding: As Double()
    - That's C-style array assignment you are using there - doesn't work in VB
    [code]
    Private Function getStats(ByVal limDist As Double) As _
    Double[color=blue]()[/color]
    ...
    Dim arrStats(3) As Double [color=Green]'0 To 3 -> 4 elements[/color]
    arrStats(0) = avgDist
    arrStats(1) = avgBearg
    arrStats(2) = minDist
    arrStats(3) = maxDist
    getStats = arrStats
    [/code]

    But I suggest you do it using ByRef variables:
    [code]
    Private [color=Blue]Sub[/color] getStats(ByVal limDist As Double, _
    [color=Blue]ByRef[/color] AverageDist As Double, [color=Blue]ByRef[/color] AverageBearg As Double, _
    [color=Blue]ByRef[/color] MinimumDist As Double, [color=Blue]ByRef[/color] MaximumDist As Double, _
    )
    ...
    [color=Green]'Return values:[/color]
    AverageDist = avgDist
    AverageBearg = avgBearg
    MinimumDist = minDist
    MaximumDist = maxDist
    End Sub
    [/code]

    Now you can call your function like:
    [code]
    ...
    Dim limDist As Double
    Dim avgDist As Double, avgBearg As Double
    Dim minDist As Double, maxDist As Double
    limDist = [italic]SomeAppropriateValue[/italic]
    getStats(limDist, avgDist, avgBearg, minDist, maxDist)
    MsgBox("Average Distance is " & avgDist)
    [/code]

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • snooglets7snooglets7 Posts: 5Member
    Thanks!! Worked perfectly...now to work out how to account for exceptions

    Kate
Sign In or Register to comment.