dbTalk Databases Forums  

Getting more than one result from a function.?

comp.databases.ms-access comp.databases.ms-access


Discuss Getting more than one result from a function.? in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gerry Abbott
 
Posts: n/a

Default Getting more than one result from a function.? - 07-07-2005 , 05:37 AM






Hi All,
If completed a script which parses a string into fragments (fields), then
assigns these substrings into an array.

I wish to turn this into a function to which i can pass the string. However
i don't know how to get the contents of the array out?
I am used to passing parameters to a function, and getting a single result
back. Is there a way to get multiple results from a function?
Is it something to do with passing byVal or byRef?


Thanks in advance,

Gerry Abbott




Reply With Quote
  #2  
Old   
rkc
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-07-2005 , 05:59 AM






Gerry Abbott wrote:
Quote:
Hi All,
If completed a script which parses a string into fragments (fields), then
assigns these substrings into an array.

I wish to turn this into a function to which i can pass the string. However
i don't know how to get the contents of the array out?
I am used to passing parameters to a function, and getting a single result
back. Is there a way to get multiple results from a function?
Is it something to do with passing byVal or byRef?
You can do it by passing in a string array or a variant byref
and having the method put values in the array.

Sub getByRef(ByRef s() As String)
Dim a(5) As String
a(0) = "yellow"
a(1) = "orange"
a(2) = "blue"
a(3) = "lemon"
a(4) = "tangerine"
a(5) = "pink"

s = a
End Sub

Sub testGetByRef()
Dim s() As String
Dim i As Integer
Call getByRef(s)

For i = 0 To UBound(s)
Debug.Print s(i)
Next
End Sub

You can also just return an array as the result of the function.

Function getStringArray() As String()
Dim a(5) As String
a(0) = "yellow"
a(1) = "orange"
a(2) = "blue"
a(3) = "lemon"
a(4) = "tangerine"
a(5) = "pink"

getStringArray = a
End Function

Sub testGetStringArray()
Dim s() As String
Dim i As Integer
s = getStringArray

For i = 0 To UBound(s)
Debug.Print s(i)
Next
End Sub



Reply With Quote
  #3  
Old   
Justin Hoffman
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-07-2005 , 06:08 AM



"Gerry Abbott" <please (AT) ask (DOT) ie> wrote

Quote:
Hi All,
If completed a script which parses a string into fragments (fields), then
assigns these substrings into an array.

I wish to turn this into a function to which i can pass the string.
However i don't know how to get the contents of the array out?
I am used to passing parameters to a function, and getting a single result
back. Is there a way to get multiple results from a function?
Is it something to do with passing byVal or byRef?


Thanks in advance,

Gerry Abbott
There was a recent discussion on ByVal/ByRef concerning how function
arguments are normally passed ByRef (unless you specify ByVal). This means
that the function can change those actual values and you could use this as a
way to get multiple values from a function - that is, you would get a single
result to say whether all the values had been loaded into the parameters you
passed:

Public Function GetMyValues(strFirstName As String, strLastName As String,
dteDOB As Date) As Boolean

' Do complicated processing to work out values
strFirstName="Steve"
strLastName="Smith"
dteDOB=DateSerial(1965,01,01)
' Return True to say we have set the values
GetMyValues=True

End Function

But this is not a method I have seen used very often. You could get you
function to return a string like:
"FirstName=Steve~LastName=Smith~DOB=1965-01-01" and split it up later. Or
you could get the function to return an array.

Or how about a user-defined data type, eg

Type Person
FirstName As String
LastName As String
DOB As Date
End Type

Public Function LoadPerson(psn As Person) As Boolean

psn.FirstName = "Steve"
psn.LastName = "Smith"
psn.DOB = DateSerial(1965, 1, 1)

LoadPerson = True

End Function

Public Sub TestPerson()

Dim psn As Person

If LoadPerson(psn) Then
MsgBox "hello I'm " & psn.FirstName & " " & psn.LastName
End If

End Sub








Reply With Quote
  #4  
Old   
Gerry Abbott
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-07-2005 , 07:39 AM



Thanks guys for the sharp response,
I had worked out the array passing, and my function is working properly this
way.

The solution you suggest using the user defined type looks like the most
elequent one for apps where the array is not appripriate. I'll give this one
a try for future reference. Need to work out which is quicker, array or
type. Also like how you suggest to set the function as a bolean type,
setting its value at the end of the function, then calling the function with
an expression. Neat.


Gerry

"Justin Hoffman" <j@b.com> wrote

Quote:
"Gerry Abbott" <please (AT) ask (DOT) ie> wrote in message
news:VB7ze.2134$R5.526 (AT) news (DOT) indigo.ie...
Hi All,
If completed a script which parses a string into fragments (fields), then
assigns these substrings into an array.

I wish to turn this into a function to which i can pass the string.
However i don't know how to get the contents of the array out?
I am used to passing parameters to a function, and getting a single
result back. Is there a way to get multiple results from a function?
Is it something to do with passing byVal or byRef?


Thanks in advance,

Gerry Abbott

There was a recent discussion on ByVal/ByRef concerning how function
arguments are normally passed ByRef (unless you specify ByVal). This
means that the function can change those actual values and you could use
this as a way to get multiple values from a function - that is, you would
get a single result to say whether all the values had been loaded into the
parameters you passed:

Public Function GetMyValues(strFirstName As String, strLastName As String,
dteDOB As Date) As Boolean

' Do complicated processing to work out values
strFirstName="Steve"
strLastName="Smith"
dteDOB=DateSerial(1965,01,01)
' Return True to say we have set the values
GetMyValues=True

End Function

But this is not a method I have seen used very often. You could get you
function to return a string like:
"FirstName=Steve~LastName=Smith~DOB=1965-01-01" and split it up later. Or
you could get the function to return an array.

Or how about a user-defined data type, eg

Type Person
FirstName As String
LastName As String
DOB As Date
End Type

Public Function LoadPerson(psn As Person) As Boolean

psn.FirstName = "Steve"
psn.LastName = "Smith"
psn.DOB = DateSerial(1965, 1, 1)

LoadPerson = True

End Function

Public Sub TestPerson()

Dim psn As Person

If LoadPerson(psn) Then
MsgBox "hello I'm " & psn.FirstName & " " & psn.LastName
End If

End Sub









Reply With Quote
  #5  
Old   
Chuck Grimsby
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-07-2005 , 05:40 PM



On Thu, 07 Jul 2005 10:59:21 GMT, rkc
<rkc (AT) rochester (DOT) yabba.dabba.do.rr.bomb> wrote:

Quote:
Gerry Abbott wrote:
Hi All,
If completed a script which parses a string into fragments (fields), then
assigns these substrings into an array.

I wish to turn this into a function to which i can pass the string. However
i don't know how to get the contents of the array out?
I am used to passing parameters to a function, and getting a single result
back. Is there a way to get multiple results from a function?
Is it something to do with passing byVal or byRef?

You can do it by passing in a string array or a variant byref
and having the method put values in the array.

Sub getByRef(ByRef s() As String)
Dim a(5) As String
a(0) = "yellow"
a(1) = "orange"
a(2) = "blue"
a(3) = "lemon"
a(4) = "tangerine"
a(5) = "pink"

s = a
End Sub

Sub testGetByRef()
Dim s() As String
Dim i As Integer
Call getByRef(s)

For i = 0 To UBound(s)
Debug.Print s(i)
Next
End Sub

You can also just return an array as the result of the function.

Function getStringArray() As String()
Dim a(5) As String
a(0) = "yellow"
a(1) = "orange"
a(2) = "blue"
a(3) = "lemon"
a(4) = "tangerine"
a(5) = "pink"

getStringArray = a
End Function

Sub testGetStringArray()
Dim s() As String
Dim i As Integer
s = getStringArray

For i = 0 To UBound(s)
Debug.Print s(i)
Next
End Sub
You can also pass a bunch of variables to a function as a ParamArray:

(This example was actually written by Joe Foster, of "Nuke Me Zemu"
legend)

Function RowAvg(ParamArray Stuff() As Variant) As Variant
Dim c As Integer: c = 0
Dim s As Variant
Dim i As Integer
For i = LBound(Stuff) To UBound(Stuff)
If IsMissing(Stuff(i)) Then
' skip it
ElseIf IsNull(Stuff(i)) Then
' skip it too
Else
s = s + Stuff(i)
c = c + 1
End If
Next
If c > 0 Then RowAvg = s / c Else RowAvg = Null
End Function


--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing



Reply With Quote
  #6  
Old   
rkc
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-07-2005 , 09:45 PM



Chuck Grimsby wrote:

Quote:
You can also pass a bunch of variables to a function as a ParamArray:

(This example was actually written by Joe Foster, of "Nuke Me Zemu"
legend)

Function RowAvg(ParamArray Stuff() As Variant) As Variant
Dim c As Integer: c = 0
Dim s As Variant
Dim i As Integer
For i = LBound(Stuff) To UBound(Stuff)
If IsMissing(Stuff(i)) Then
' skip it
ElseIf IsNull(Stuff(i)) Then
' skip it too
Else
s = s + Stuff(i)
c = c + 1
End If
Next
If c > 0 Then RowAvg = s / c Else RowAvg = Null
End Function

That's nice, but using an array of a defined type would
clear up what the function actually expected to be passed
in. It's tough to guarantee a result when your only argument
will except any number of any data type, including objects.






Reply With Quote
  #7  
Old   
Chuck Grimsby
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-08-2005 , 04:17 AM



On Fri, 08 Jul 2005 02:45:12 GMT, rkc
<rkc (AT) rochester (DOT) yabba.dabba.do.rr.bomb> wrote:

Quote:
Chuck Grimsby wrote:
You can also pass a bunch of variables to a function as a ParamArray:
(This example was actually written by Joe Foster, of "Nuke Me Zemu"
legend)
Function RowAvg(ParamArray Stuff() As Variant) As Variant

That's nice, but using an array of a defined type would
clear up what the function actually expected to be passed
in. It's tough to guarantee a result when your only argument
will except any number of any data type, including objects.
Personally, I quite agree! Indeed, I tend to write code in exactly
that fashion myself, but I also think it's important to know _all_ the
options, not just those I happen to agree with.


--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing



Reply With Quote
  #8  
Old   
rkc
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-08-2005 , 06:16 AM



Chuck Grimsby wrote:
Quote:
On Fri, 08 Jul 2005 02:45:12 GMT, rkc
rkc (AT) rochester (DOT) yabba.dabba.do.rr.bomb> wrote:


Chuck Grimsby wrote:

You can also pass a bunch of variables to a function as a ParamArray:
(This example was actually written by Joe Foster, of "Nuke Me Zemu"
legend)
Function RowAvg(ParamArray Stuff() As Variant) As Variant


That's nice, but using an array of a defined type would
clear up what the function actually expected to be passed
in. It's tough to guarantee a result when your only argument
will except any number of any data type, including objects.


Personally, I quite agree! Indeed, I tend to write code in exactly
that fashion myself, but I also think it's important to know _all_ the
options, not just those I happen to agree with.
You're right. So let's add that a paramarray is always optional, must
always be the last argument in the list and is always passed ByVal.
Always being passed ByVal means it can't be used as a way to
return values to the calling code.





Reply With Quote
  #9  
Old   
DenBorg
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-08-2005 , 07:00 AM



rkc wrote:
Quote:
You're right. So let's add that a paramarray is always optional, must
always be the last argument in the list and is always passed ByVal.
Always being passed ByVal means it can't be used as a way to
return values to the calling code.
Correct, except for the part stating that ParamArray arguments are
always passed ByVal and that it can't be used to return values to the
calling code.



Reply With Quote
  #10  
Old   
Thelma Lubkin
 
Posts: n/a

Default Re: Getting more than one result from a function.? - 07-08-2005 , 07:31 AM



rkc <rkc (AT) rochester (DOT) yabba.dabba.do.rr.bomb> wrote:
: Chuck Grimsby wrote:
:> On Fri, 08 Jul 2005 02:45:12 GMT, rkc
:> <rkc (AT) rochester (DOT) yabba.dabba.do.rr.bomb> wrote:
:>
:>
:>>Chuck Grimsby wrote:
:>>
:>>>You can also pass a bunch of variables to a function as a ParamArray:
:>>>(This example was actually written by Joe Foster, of "Nuke Me Zemu"
:>>>legend)
:>>>Function RowAvg(ParamArray Stuff() As Variant) As Variant
: You're right. So let's add that a paramarray is always optional, must
: always be the last argument in the list and is always passed ByVal.
: Always being passed ByVal means it can't be used as a way to
: return values to the calling code.

Does the ParamArray type have any useful function other than
reminding the coder what [s]he's doing? I would write such
code using an ordinary array and restricting it for the
functionality that I needed. Maybe that's because I've never
written code for a large project where it's assumed that
someone else will need to expand and maintain it.
--thelma

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.