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
  #11  
Old   
lylefair@yahoo.ca
 
Posts: n/a

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






I suppose that we could say that we can pass arrays as elements of the
ParamArray and that those array elements will be passed ByRef and can
be changed (used in a way to return values to the calling code).
But this is a stretch. OTTOMH I don't see any particular use for this,
unless perhaps we might want to process a series of arrays.

Sub temp1(ParamArray a())
a(0)(2) = StrConv(a(0)(2), vbUpperCase)
End Sub

Sub temp2()
Dim b(4)
b(2) = "the"
temp1 b
Debug.Print b(2)
'Displays "THE"
End Sub

I haven't checked this out but I'm guessing that if we send an object,
say a recordset, as an element of a ParamArray then it would be passed
ByRef and that we could modify it directly in the Called Proecdure. Is
there any use for this? I can't think of one.


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

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






RTK incorrectly stated that the modified values of elements passed into
a ParamArray is not and cannot be returned to the calling routine.

WHY do you think that the original values are left unchanged? No need
to resort to passing Arrays of Arrays just to get the original values
to be changed.

Try out the following code, call Demo and see what happens to the
values of S1, S2, and S3.

Add the following to a Standard Code Module (i.e. not a class module),
and in the immediate window, type "Demo" and press Return:


Public Sub ParmArraySub(ParamArray X() As Variant)
Dim I As Long

For I = 0 To UBound(X)
If I <> 1 Then
X(I) = UCase$(X(I)) 'Convert to Upper Case
Else
X(I) = "Modified!!!"
End If
Next 'I
End Sub

Public Sub Demo()
Dim S1 As String
Dim S2 As String
Dim S3 As String

S1 = "abc"
S2 = "jkl"
S3 = "xyz"
ParmArraySub S1, S2, S3
MsgBox S1
MsgBox S2
MsgBox S3
End Sub


Reply With Quote
  #13  
Old   
lylefair@yahoo.ca
 
Posts: n/a

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



It seems you are right. Thank you for the clear demonstration.


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

Default Re: Getting more than one result from a function.? - 07-08-2005 , 02:29 PM



DenBorg wrote:
Quote:
RTK incorrectly stated that the modified values of elements passed into
a ParamArray is not and cannot be returned to the calling routine.
I stand corrected.


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

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



On 8 Jul 2005 12:31:54 GMT, Thelma Lubkin <thelma (AT) alpha2 (DOT) csd.uwm.edu>
wrote:

Quote:
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.
To be honest, the only time I've ever implemented a ParamArray is when
I was going to use the function in a query where I wasn't _always_
going to know how many variables I was going to be passing to the
query.

For example, a query where I was selecting on a crosstab where I
wouldn't know how many columns were going to be there, or how many
fields would have Null values. Passing the columns I needed to do
<whatever> with as a ParamArray resolved that issue, however I did
have to handle the Nulls in the code of the function.

Even still, I've always thought of it as a bit of a "Hack". But it
works, it solved the problem, and is rather stable. So I guess
there's no real problem using them, I'm just not a big fan.

Personally, I never use Subs to modify an existing array (or any other
kind of variable for that matter), it's just not something I do, so
that's never an issue with me. (I prefer to have a function return
something, as it's _far_ clearer to programmers "down the line" who
may have to work on my code. Feel free to do as you feel the need to
do however. I don't begrudge the people who do this, it's just not
something *I* do.)

I should also mention that I rarely pass arrays around anyways.
Again, no real reason, it's just something I don't do. I'd rather use
a Type, so that I _know_ (and have semi-documented in the Type's
Declare statement) what is supposed to be in what element.

When passing _anything_ _anywhere_ however, I try to be as explicit as
possible when I write the function. I also (perhaps sadly) tend to
leave a lot of "dead" code with lots of comments on the dead code, and
what the (working) code is doing. This not only helps the programmers
down the line, but me as well, when I have to go back to the code
several years (months, projects) later and make a fix. The "School of
Hard Knocks" has some fairly harsh teachers!


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



Reply With Quote
  #16  
Old   
David Schofield
 
Posts: n/a

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



On Thu, 07 Jul 2005 17:40:44 -0500, Chuck Grimsby
<c.grimsby (AT) worldnet (DOT) att.net.invalid> 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)

snip

Hi
It was "Nuke me Xemu"

After reading
http://www.googlism.com/what_is/x/xemu/
I am none the wiser though it seems to be related to scientology.

On the subject of the post I was going to suggest returning a private
type but by the time I had looked for Xemu I see someone has done
that. Types (structures) have rather gone out of fashion, displaced by
objects, which is odd for database users - after all a recordset is
only an a collection or array of structures. VB has no way of
automatically creating a type to match a recordset, and disconnected
recordsets were introduced as an out-of-language feature as though
they were something new.

David



Reply With Quote
  #17  
Old   
David W. Fenton
 
Posts: n/a

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



Chuck Grimsby <c.grimsby (AT) worldnet (DOT) att.net.invalid> wrote in
news:uhgsc11h66nme01qvq474qcqs0slfp98ij (AT) 4ax (DOT) com:

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.
Passing a parameter array, though, is extremely useful for cases
where you want to pass multiple values, all of the same type, but
don't know until runtime exactly how many. It's a stupid example,
but what if you had a function that concatenated a list of items and
returned them as a list. With a parameter array, you don't need to
know how many items your passing in.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc


Reply With Quote
  #18  
Old   
David W. Fenton
 
Posts: n/a

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



Thelma Lubkin <thelma (AT) alpha2 (DOT) csd.uwm.edu> wrote in
news:dalrnq$r72$1 (AT) uwm (DOT) edu:

Quote:
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.
I answered this question in a previous post, in a context where my
answer didn't make much sense.

I wrote:

Passing a parameter array, though, is extremely useful for cases
where you want to pass multiple values, all of the same type, but
don't know until runtime exactly how many. It's a stupid example,
but what if you had a function that concatenated a list of items and
returned them as a list. With a parameter array, you don't need to
know how many items your passing in.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc


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

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



David W. Fenton wrote:

Quote:
I answered this question in a previous post, in a context where my
answer didn't make much sense.

I wrote:

Passing a parameter array, though, is extremely useful for cases
where you want to pass multiple values, all of the same type, but
don't know until runtime exactly how many. It's a stupid example,
but what if you had a function that concatenated a list of items and
returned them as a list. With a parameter array, you don't need to
know how many items your passing in.
The only advantage I can find that a paramarray has over an array as
a parameter is that using ubound on an uninitialized paramarray returns
-1 instead of throwing an error. But then IsMissing() gives you the
same information without having to know that.


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.