Function return values

T

thomasp

Is it possible to have a function that is called with two variables:

Public Function funDirDist(ByVal strPOO As String, ByVal strPOI As String)
As Integer

and have it return an array with 3 values?

Thanks,

Thomas
 
G

Guest

Yes. A function returning an array of integers looks like this:

Public Function funDirDist(ByVal strPOO As String, ByVal strPOI As String)
As Integer()
dim Data(whatever) as integer
' whatever
Return Data
End Function
 
J

Jay B. Harlow [MVP - Outlook]

Thomas,
Have you tried:
| Public Function funDirDist(ByVal strPOO As String, ByVal strPOI As String)
| As Integer()
Dim r(2) As Integer
r(0) = 1
r(1) = 2
r(2) = 3
Return r
End Function

Dim value() As Integer
value = funDirDist("x", "y")

Hope this helps
Jay



|
| Is it possible to have a function that is called with two variables:
|
| Public Function funDirDist(ByVal strPOO As String, ByVal strPOI As String)
| As Integer
|
| and have it return an array with 3 values?
|
| Thanks,
|
| Thomas
|
| --
| Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
| ------->>>>>>http://www.NewsDemon.com<<<<<<------
| Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
B

Bernie Yaeger

Hi Thomas,

Just pass it an array as integer by ref:
public function fundirdist(byref poarray() as integer) as integer
etc

HTH,

Bernie Yaeger
 
T

thomasp

Ok, I was headed in the correct direction, but I still don't know how to
call it.
I am trying to combine two functions that I use quite often back to back

I call them as:
distance = funDistance(strPOO, StrPOI)
direction = funDirection(strPOO, StrPOI)

How would I call funDirDist?

intError = data(0)
intDistance = data(1)
intDirection = data(2)

?????????

Public Function funDirDist(ByVal strPOO As String, ByVal strPOI As String)
As Integer()

dim Data(2) as integer

data(0) = int indicating if there was an error
data(1) = int indicating distance
data(2) = int indicating direction

Return Data

End Function


One other question. I pass the numbers as strings to my functions because
they sometimes start with a zero
and if I pass them as integers the zero gets lost. Is there another was
around that. This does seem to work though.

Thanks for all the help,

Thomas
 
H

Herfried K. Wagner [MVP]

Ok, I was headed in the correct direction, but I still don't know how to
call it.
I am trying to combine two functions that I use quite often back to back

I call them as:
distance = funDistance(strPOO, StrPOI)
direction = funDirection(strPOO, StrPOI)

How would I call funDirDist?

intError = data(0)
intDistance = data(1)
intDirection = data(2)

?????????

Yes, that's the way to go.
One other question. I pass the numbers as strings to my functions because
they sometimes start with a zero
and if I pass them as integers the zero gets lost. Is there another was
around that. This does seem to work though.

This depends on what semantics the numers have. If they are used to perform
calculations, leading zeros are not necessary and it's prefered to use a
numeric data type to pass the values.
 
M

Mythran

Ok, I was headed in the correct direction, but I still don't know how to
call it.
I am trying to combine two functions that I use quite often back to back

I call them as:
distance = funDistance(strPOO, StrPOI)
direction = funDirection(strPOO, StrPOI)

How would I call funDirDist?

Public Function funDirDist(ByVal POO As Double, ByVal POI As String) As
Integer()
... processing for setting the return array ...
Return New Integer() { _
int indicating if there was an error, _
int indicating distance, _
int indicating direction _
}
End Function

.... to call ...

Dim p00 As Double = Double.Parse(strPOO)
Dim pOI As Double = Double.Parse(strPOI)
Dim result As Integer() = funDirDist(pOO, pOI)
Dim intError As Integer = result(0)
Dim intDistance As Integer = result(1)
Dim intDirection As Integer = result(2)

.... or use a structure ...
Structure DirDistStructure
Public [Error] As Integer
Public Distance As Integer
Public Direction As Integer
End Structure

Public Function funDirDist(ByVal POO As Double, ByVal POI As String) As
DirDistStructure
Dim st As DirDistStructure
... processing for setting the return values ...
st.Error = int indicating if there was an error
st.Distance = int indicating distance
st.Direction = int indicating direction
End Function

.... to call ...

Dim p00 As Double = Double.Parse(strPOO)
Dim pOI As Double = Double.Parse(strPOI)
Dim result As DirDistStructure = funDirDist(pOO, pOI)
Dim intError As Integer = result.Error
Dim intDistance As Integer = result.Distance
Dim intDirection As Integer = result.Direction

One other question. I pass the numbers as strings to my functions because
they sometimes start with a zero
and if I pass them as integers the zero gets lost. Is there another was
around that. This does seem to work though.

Thanks for all the help,

Thomas

Double, would be what I would use. Double.Parse to parse the numeric string
into a double, as the example above shows ;)

HTH,
Mythran
 
T

thomasp

If I use this function quite often, would it be worthwhile to create a .dll
file?

Thanks,

Thomas
 
J

Jay B. Harlow [MVP - Outlook]

Thomas,
If you used this function quite often, you may want to seriously consider
Replace Array with Object:

http://www.refactoring.com/catalog/replaceArrayWithObject.html

As the new type that is returned better documents what the 3 values being
returned are.

Then put the new function that returns a specific object in a Class Library
(.dll).

Public Class Result

' this class is immutable,
' remove the Readonly if you want mutable values
Public Readonly Error As Integer
Public Readonly Distance As Integer
Public Readonly Direction As Integer

Public Sub New(error As Integer, distance As Integer, direction As
Integer)
Me.Error = error
Me.Distance = distance
Me.direction = direction
End Sub

End Class

Public Function funDirDist(ByVal POO As Double, ByVal POI As String) As
Integer()
... processing for setting the return array ...
Dim theResult As New Result( _
int indicating if there was an error, _
int indicating distance, _
int indicating direction _
)
End Function


Dim theResult As Result = funDirDist(pOO, pOI)
Dim intError As Integer = result.Error
Dim intDistance As Integer = result.Distance
Dim intDirection As Integer = result.Direction

I would consider having the funDirDist method part of the Result class,
possible as a Shared FromSomething method (the Something is what the POO &
POI represent). The Result class possible should not contain Error (I would
use Exceptions to convey errors, rather then return values). I would also
consider naming Result: Vector or DistanceDirection or something more
indicative of what it is.

Public Class Vector
Public Readonly Distance As Integer
Public Readonly Direction As Integer
...
End Class

I would then put the logic that is specific to a Result/Vector in the
Result/Vector class.

Hope this helps
Jay

|
| If I use this function quite often, would it be worthwhile to create a
..dll
| file?
|
| Thanks,
|
| Thomas
|
| --
| Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
| ------->>>>>>http://www.NewsDemon.com<<<<<<------
| Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
L

Larry Lard

Jay said:
Thomas,
If you used this function quite often, you may want to seriously consider
Replace Array with Object:

http://www.refactoring.com/catalog/replaceArrayWithObject.html

As the new type that is returned better documents what the 3 values being
returned are.

Then put the new function that returns a specific object in a Class Library
(.dll).

Public Class Result

' this class is immutable,
' remove the Readonly if you want mutable values
Public Readonly Error As Integer
Public Readonly Distance As Integer
Public Readonly Direction As Integer

Public Sub New(error As Integer, distance As Integer, direction As
Integer)
Me.Error = error
Me.Distance = distance
Me.direction = direction
End Sub

End Class

Jay,

My instinct would be to make this a Structure, not a Class, as it feels
like a value-type. What would your thoughts be on that?

[context snipped since no longer directly relevant]
 
J

Jay B. Harlow [MVP - Outlook]

Doh!

| Public Function funDirDist(ByVal POO As Double, ByVal POI As String) As
Result
| ... processing for setting the return array ...
| Dim theResult As New Result( _
| int indicating if there was an error, _
| int indicating distance, _
| int indicating direction _
| )
Return theResult
| End Function

We need to actually return the new value we are creating... ;-)

Jay


| Thomas,
| If you used this function quite often, you may want to seriously consider
| Replace Array with Object:
|
| http://www.refactoring.com/catalog/replaceArrayWithObject.html
|
| As the new type that is returned better documents what the 3 values being
| returned are.
|
| Then put the new function that returns a specific object in a Class
Library
| (.dll).
|
| Public Class Result
|
| ' this class is immutable,
| ' remove the Readonly if you want mutable values
| Public Readonly Error As Integer
| Public Readonly Distance As Integer
| Public Readonly Direction As Integer
|
| Public Sub New(error As Integer, distance As Integer, direction As
| Integer)
| Me.Error = error
| Me.Distance = distance
| Me.direction = direction
| End Sub
|
| End Class
|
| Public Function funDirDist(ByVal POO As Double, ByVal POI As String) As
| Integer()
| ... processing for setting the return array ...
| Dim theResult As New Result( _
| int indicating if there was an error, _
| int indicating distance, _
| int indicating direction _
| )
| End Function
|
|
| Dim theResult As Result = funDirDist(pOO, pOI)
| Dim intError As Integer = result.Error
| Dim intDistance As Integer = result.Distance
| Dim intDirection As Integer = result.Direction
|
| I would consider having the funDirDist method part of the Result class,
| possible as a Shared FromSomething method (the Something is what the POO &
| POI represent). The Result class possible should not contain Error (I
would
| use Exceptions to convey errors, rather then return values). I would also
| consider naming Result: Vector or DistanceDirection or something more
| indicative of what it is.
|
| Public Class Vector
| Public Readonly Distance As Integer
| Public Readonly Direction As Integer
| ...
| End Class
|
| I would then put the logic that is specific to a Result/Vector in the
| Result/Vector class.
|
| Hope this helps
| Jay
|
<<snip>>
 
J

Jay B. Harlow [MVP - Outlook]

Larry,
| My instinct would be to make this a Structure, not a Class, as it feels
| like a value-type. What would your thoughts be on that?

Yes! Based on what little is given here, yes this would be a good candidate
for a Structure.

As structures are "best" reserved for types that:
- act like primitive types
- have an instance size under 16 bytes
- are immutable
- value semantics are desirable

http://msdn.microsoft.com/library/d...genref/html/cpconValueTypeUsageGuidelines.asp

Which is part of the reason I say "a new type", as Type here can be either a
Class or Structure based on the requirements that the Type must meet...

Hope this helps
Jay

|
|
| Jay B. Harlow [MVP - Outlook] wrote:
| > Thomas,
| > If you used this function quite often, you may want to seriously
consider
| > Replace Array with Object:
| >
| > http://www.refactoring.com/catalog/replaceArrayWithObject.html
| >
| > As the new type that is returned better documents what the 3 values
being
| > returned are.
| >
| > Then put the new function that returns a specific object in a Class
Library
| > (.dll).
| >
| > Public Class Result
| >
| > ' this class is immutable,
| > ' remove the Readonly if you want mutable values
| > Public Readonly Error As Integer
| > Public Readonly Distance As Integer
| > Public Readonly Direction As Integer
| >
| > Public Sub New(error As Integer, distance As Integer, direction
As
| > Integer)
| > Me.Error = error
| > Me.Distance = distance
| > Me.direction = direction
| > End Sub
| >
| > End Class
|
| Jay,
|
| My instinct would be to make this a Structure, not a Class, as it feels
| like a value-type. What would your thoughts be on that?
|
| [context snipped since no longer directly relevant]
|
| --
| Larry Lard
| Replies to group please
|
 
T

thomasp

Here is the code for the two functions. Distance is called first with two
grid cordinates.
example: strPOI = 4800030000 ---This is the Point of Impact
strPOO = 4800036000 --- This is the Point of Origin

Function Distance would return 6000 meters. It would also set a value to
diffEast = 0 and diffNorth = 6000

Function Direction is then called with 0 and 6000 and would return a value
of 6400 mils since the direction is due north.

I am working on using regular expression to improve how I trap incorrect
inputs from the user and when number are imported from other files. Since I
was working on improving the code, I just wanted to combine the two and
create a .dll file that I can use with other projects. I am not a
programer, may give it a run when I retire from the miliary. All I am
trying to do now is make all the data my radar sections here in Iraq
manageable.

The link to my website is below. It is full of pictures of some of the
stuff I do over here.

Thanks for all your help,

SFC, MSARNG
Target Acquisition Platoon Seargeant
Visit my webpage: http://www.msala.net/archives.php


Public Function Distance(ByVal strPOO As String, ByVal strPOI As String,
ByRef diffEast As Double, ByRef diffNorth As Double) As Double

Dim dblEast2 As Double
Dim dblEast1 As Double
Dim dblNorth2 As Double
Dim dblNorth1 As Double
Dim bolValid As Boolean

bolValid = False

If Len(Trim(strPOO)) = 10 Then
If Len(Trim(strPOI)) = 10 Then
bolValid = True
End If
End If

If bolValid = True Then

dblEast1 = Val(Left(strPOO, 5))
dblEast2 = Val(Left(strPOI, 5))
dblNorth1 = Val(Right(strPOO, 5))
dblNorth2 = Val(Right(strPOI, 5))

diffEast = dblEast1 - dblEast2

Select Case diffEast
Case Is > 49000
diffEast = 100000 - diffEast
Case Is < -49000
diffEast = 100000 + diffEast
diffEast = diffEast - (diffEast * 2)
Case Else
diffEast = diffEast
End Select

diffNorth = dblNorth1 - dblNorth2

Select Case diffNorth
Case Is > 49000
diffNorth = 100000 - diffNorth
Case Is < -49000
diffNorth = 100000 + diffNorth
diffNorth = diffNorth - (diffNorth * 2)
Case Else
diffNorth = diffNorth
End Select

Distance = Round(Math.Sqrt((dblEast2 - dblEast1) ^ 2 +
(dblNorth2 - dblNorth1) ^ 2), 0)

Else : Distance = 0

End If

End Function

Public Function Direction(ByVal diffNorth As Double, ByVal diffEast As
Double) _
As Double

'Use IF THEN ELSE to determine which quadrant the target is in
'and call that quadrant funtion to calculate the direction

'Determines quadrant 1
If (diffNorth > 0 Or diffNorth = 0) And _
(diffEast > 0 Or diffEast = 0) Then
Direction = Round(Quadrant1(diffNorth, diffEast), 0)
Else
'Determines quadrant 2
If (diffNorth < 0 Or diffNorth = 0) And _
(diffEast > 0 Or diffEast = 0) Then
Direction = Round(Quadrant2(diffNorth, diffEast), 0)
Else
'Determines quadrant 3
If (diffNorth < 0 Or diffNorth = 0) And _
(diffEast < 0 Or diffNorth = 0) Then
Direction = Round(Quadrant3(diffNorth, diffEast), 0)
Else
'Determines quadrant 4
If (diffNorth > 0 Or diffNorth = 0) And _
(diffEast < 0 Or diffEast = 0) Then
Direction = Round(Quadrant4(diffNorth, diffEast), 0)
End If
End If
End If
End If

End Function

Private Function Quadrant1(ByVal diffNorth As Double, _
ByVal diffEast As Double) As Double

'Assign direction value to due North(6400) and due East(1600)
If diffNorth = 0 Then
Slope = 1600
Else
If diffEast = 0 Then
Slope = 6400

'Caculate direction values between 6400 and 1600 using
'the ArcTan of slope formula
Else
Slope = diffEast / diffNorth
Slope = Math.Atan(Slope) / 0.000981747
End If
End If

'Round slope and pass it back to Direction Function as Quadrant1
Quadrant1 = Round(Slope, 1)
End Function

Private Function Quadrant2(ByVal diffNorth As Double, _
ByVal diffEast As Double) As Double

'Assign direction value to due South(3200) and due East(1600)
If diffNorth = 0 Then
Slope = 1600
Else
If diffEast = 0 Then
Slope = 3200

'Caculate direction values between 1600 and 3200 using
'the ArcTan of slope formula
Else
Slope = diffNorth / diffEast
Slope = Math.Atan(Slope) / 0.000981747
Slope = Abs(Slope) + 1600
End If
End If

'Round slope and pass it back to Direction Function as Quadrant2
Quadrant2 = Round(Slope, 1)
End Function

Private Function Quadrant3(ByVal diffNorth As Double, _
ByVal diffEast As Double) As Double

'Assign direction value to due South(3200) and due West(4800)
If diffNorth = 0 Then
Slope = 4800
Else
If diffEast = 0 Then
Slope = 3200

'Caculate direction values between 3200 and 4800 using
'the ArcTan of slope formula
Else
Slope = diffEast / diffNorth
Slope = Math.Atan(Slope) / 0.000981747
Slope = Abs(Slope) + 3200
End If
End If

'Round slope and pass it back to Direction Function as Quadrant3
Quadrant3 = Round(Slope, 1)

End Function

Private Function Quadrant4(ByVal diffNorth As Double, _
ByVal diffEast As Double) As Double

'Assign direction value to due North(6400) and due West(4800)
If diffNorth = 0 Then
Slope = 4800
Else
If diffEast = 0 Then
Slope = 6400

'Caculate direction values between 4800 and 6400 using
'the ArcTan of slope formula
Else
Slope = diffNorth / diffEast
Slope = Math.Atan(Slope) / 0.000981747
Slope = Abs(Slope) + 4800
End If
End If

'Round slope and pass it back to Direction Function as Quadrant4
Quadrant4 = Round(Slope, 1)

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top