Functions: Passing multiple values

D

Dennis D.

Hello:

I want a function to return three variables to the calling procedure:

Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as
Integer, ByVal iAddMins as Integer) As Array

Variable values are calculated in the function.

Calling procedure receives the values preferably into variables of the same
name.

Like: Me.CalcTimes(iAddDays, iAddHours, iAddMins)

Further use is ...(New TimeSpan(iAddDays, iAddHours, iAddMins, 0))

Could use ParamArray Arguments?

Is As Array correct or should it be As Integer?
Do I need to declare CalcTimes as an array before it is used, or is it an
array by definition?

This is somewhat confusing to me.
Balena's Prog-Vb ParamArray example begins:
Function Sum(ParamArray ByVal args() As Integer) As Integer
Dim index As Integer
For index = 0 to UBound(args)....

So far, I'm getting a debug error. I tried a return statement:
Return CalcTimes(iAddDays, iAddHours, iAddMins)
I'm not even sure about using a return statement here. It immediately
preceeds the End Function statement anyway.
Sorry about being lazy. 5:36AM is near end of the day for me.
Thanks.

D.
 
C

CJ Taylor

Make it easy on yoursefl

Public Sub myFunction (byRef iAddDays as Integer, byRef iAddHours as
Integer, byRef iAddMins as integer)

Useing Ref will pass by reference, (just passing the "pointer" in) and
allow you to modify the variables and pass all 3 back.

I have no idea what ParamArray ByVal Params() as Integer will do... never
seen ParamArray before.

-CJ
 
S

Shiva

Hi,
An easy way to get multiple return values from a function is to use ByRef
parameters. For example:

Sub CalcTimes (ByRef i As Integer, ByRef j As Integer, ByRef k As Integer)
i = 40
j = 49
k = 39
End Sub

Dim a, b, c As Integer
CalcTimes (a, b, c)

After the call, a, b & c will all have the values set by CalcTimes.

I hope this is what you are looking for.

Hello:

I want a function to return three variables to the calling procedure:

Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as
Integer, ByVal iAddMins as Integer) As Array

Variable values are calculated in the function.

Calling procedure receives the values preferably into variables of the same
name.

Like: Me.CalcTimes(iAddDays, iAddHours, iAddMins)

Further use is ...(New TimeSpan(iAddDays, iAddHours, iAddMins, 0))

Could use ParamArray Arguments?

Is As Array correct or should it be As Integer?
Do I need to declare CalcTimes as an array before it is used, or is it an
array by definition?

This is somewhat confusing to me.
Balena's Prog-Vb ParamArray example begins:
Function Sum(ParamArray ByVal args() As Integer) As Integer
Dim index As Integer
For index = 0 to UBound(args)....

So far, I'm getting a debug error. I tried a return statement:
Return CalcTimes(iAddDays, iAddHours, iAddMins)
I'm not even sure about using a return statement here. It immediately
preceeds the End Function statement anyway.
Sorry about being lazy. 5:36AM is near end of the day for me.
Thanks.

D.
 
I

Imran Koradia

If iAddDays, iAddHours and iAddmins are the values you are returning, you
can just pass them byref and the changes to these values will reflect ones
the procedure returns. In that case, you don't need to define it as
function - you could just define it as a sub.
If you are going to be using those 3 variables often - probably for a lot of
date-time calculations - you might want to define a structure with the 3
variables and then create a function that returns that structure.
something like:

private Structure DateTimeStruct
dim iAddDays as Integer
dim iAddHours as Integer
dim iAddMins as Integer
end structure

function CalcTimes( ) as DateTimeStruct

and then call it as:
dim myStruct as DateTimeStruct = Me.CalcTimes

just along those lines..

hope that helps..
Imran.
 
C

Cor Ligthert

Dennis,

First of all this text from this page
The Array class is the base class for language implementations that support
arrays. However, only the system and compilers can derive explicitly from
the Array class. Users should use the array constructs provided by the
language.

http://msdn.microsoft.com/library/d...-us/cpref/html/frlrfSystemArrayClassTopic.asp

And than you can try this sample I made from your question.

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim mydates As Date()
mydates = CalcTimes(Now.Day, Now.TimeOfDay.Hours, _
Now.TimeOfDay.Minutes)
MessageBox.Show(mydates(2).ToString)
End Sub
Private Function CalcTimes(ByVal iAddDays As _
Integer, ByVal iAddHours As Integer, _
ByVal iAddMins As Integer) As Date()
Dim mydate As New Date(2004, 8, iAddDays, _
iAddHours, iAddMins, 0)
Dim mydatetable(2) As Date
mydatetable(0) = mydate
mydate.AddDays(1)
mydatetable(1) = mydate
mydate.AddDays(1)
mydatetable(2) = mydate
Return mydatetable
End Function
///
 
J

Jay B. Harlow [MVP - Outlook]

CJ
I have no idea what ParamArray ByVal Params() as Integer will do... never
seen ParamArray before.

ParamArray allows you to pass a variable number of parameters. You use it
when you want to send a list of input parameters to the function, but you
don't know before hand how many items will be in that list. That explicitly
using an array for the call would be inconvenient, for example Console.Write
where you specify a Format string
http://msdn.microsoft.com/library/d.../html/frlrfSystemConsoleClassWriteTopic14.asp


For details on ParamArray see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec7_1_6_4.asp


For example:

Function Sum(ParamArray ByVal args() As Integer) As Integer
...
End Function


x = Sum(1)
x = Sum(1,2,3,4,5,6)
x = Sum(1,2,3)
x = Sum(6,7,8,9)

is Equivalent to:

Function Sum(ByVal args() As Integer) As Integer
...
End Function

x = Sum(New Integer() {1})
x = Sum(New Integer() {1,2,3,4,5,6})
x = Sum(New Integer() {1,2,3})
x = Sum(New Integer() {6,7,8,9})

The ParamArray allows you to skip the array initialization syntax. (cleaner
usage of Sum).

In fact ParamArray allows you to send either a variable number of parameters
or a single Array.

Hope this helps
Jay



CJ Taylor said:
Make it easy on yoursefl

Public Sub myFunction (byRef iAddDays as Integer, byRef iAddHours as
Integer, byRef iAddMins as integer)

Useing Ref will pass by reference, (just passing the "pointer" in) and
allow you to modify the variables and pass all 3 back.

I have no idea what ParamArray ByVal Params() as Integer will do... never
seen ParamArray before.

-CJ
 
C

CJ Taylor

Thanks a lot Jay. Never saw that before.

-CJ

Jay B. Harlow said:
CJ
I have no idea what ParamArray ByVal Params() as Integer will do... never
seen ParamArray before.

ParamArray allows you to pass a variable number of parameters. You use it
when you want to send a list of input parameters to the function, but you
don't know before hand how many items will be in that list. That explicitly
using an array for the call would be inconvenient, for example Console.Write
where you specify a Format string
http://msdn.microsoft.com/library/d.../html/frlrfSystemConsoleClassWriteTopic14.asp


For details on ParamArray see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec7_1_6_4.asp


For example:

Function Sum(ParamArray ByVal args() As Integer) As Integer
...
End Function


x = Sum(1)
x = Sum(1,2,3,4,5,6)
x = Sum(1,2,3)
x = Sum(6,7,8,9)

is Equivalent to:

Function Sum(ByVal args() As Integer) As Integer
...
End Function

x = Sum(New Integer() {1})
x = Sum(New Integer() {1,2,3,4,5,6})
x = Sum(New Integer() {1,2,3})
x = Sum(New Integer() {6,7,8,9})

The ParamArray allows you to skip the array initialization syntax. (cleaner
usage of Sum).

In fact ParamArray allows you to send either a variable number of parameters
or a single Array.

Hope this helps
Jay
 
D

Dennis D.

Thanks Jay. That's a good description of ParamArray. The issue of sending an
unknown number of values to a function has been around for decades in
various languages. Here it is again. That's interesting. I had forgotten it.

Back a few decades the proper way to learn a language began with a white
paper. Do they exist for dot net, or would a person begin by studying the
compiler specs? Guess what I'm asking is: Where does it all start?

Thanks again Jay,

D.
 
D

Dennis D.

Thanks Shiva:
I hadn't thought of using ByRef, but that seems the way to go.

D.

Shiva said:
Hi,
An easy way to get multiple return values from a function is to use ByRef
parameters. For example:

Sub CalcTimes (ByRef i As Integer, ByRef j As Integer, ByRef k As Integer)
i = 40
j = 49
k = 39
End Sub

Dim a, b, c As Integer
CalcTimes (a, b, c)

After the call, a, b & c will all have the values set by CalcTimes.

I hope this is what you are looking for.

It is exactly. Thanks.
 
D

Dennis D.

Thanks Imran Koradia,

I'll give it a try. It will give me the opportunity to study Structure
architecture and usage as well.

Thanks,

D.

Imran Koradia said:
If iAddDays, iAddHours and iAddmins are the values you are returning, you
can just pass them byref and the changes to these values will reflect ones
the procedure returns. In that case, you don't need to define it as
function - you could just define it as a sub.
If you are going to be using those 3 variables often - probably for a lot
of
date-time calculations - you might want to define a structure with the 3
variables and then create a function that returns that structure.
something like:

private Structure DateTimeStruct
dim iAddDays as Integer
dim iAddHours as Integer
dim iAddMins as Integer
end structure

function CalcTimes( ) as DateTimeStruct

and then call it as:
dim myStruct as DateTimeStruct = Me.CalcTimes

just along those lines..

hope that helps..
Imran.
 
D

Dennis D.

Cor thanks.
Where we be without tables? "You must make the kettle before you can make
the soup." - a friend.
Ha. Just joking.
I'll give this a try, and thanks for the URL's.

D.
 
D

Dennis D.

Thanks CJ,
Public Sub myFunction?
A little late night humor?

I'll try it and see how the calculations react to the pointers. Shouldn't be
a problem. I hadn't thought of using ByRef in this case.

Thanks,

D.

CJ Taylor said:
Make it easy on yoursefl

Public Sub myFunction (byRef iAddDays as Integer, byRef iAddHours as
Integer, byRef iAddMins as integer)

Useing Ref will pass by reference, (just passing the "pointer" in) and
allow you to modify the variables and pass all 3 back.

I have no idea what ParamArray ByVal Params() as Integer will do... never
seen ParamArray before.

-CJ
 
J

Jay B. Harlow [MVP - Outlook]

Dennis,
Guess what I'm asking is: Where does it all start?
Where does all what start?

Programming concepts, OO concepts, Patterns, Framework, Web, Database, the
VB.NET Language itself, methodology (Interaction Design vs. Extreme
Programming)?


I believe there is a white paper for C# on MSDN, I don't know of a white
paper per se for VB.NET. For both C# & VB.NET I've reviewed the "compiler
spec" in MSDN. I find Paul Vick's book "The Visual Basic .NET Programming
Language" from Addison Wesley to be a much easier read then the "compiler
spec" on MSDN. Both are useful.

James S. Miller's "The Common Language Infrastructure Annotated Standard"
also from Addison Wesley, is the "CLI spec" with annotation, which provides
a good insight into how the CLI & CLR itself works...

Hope this helps
Jay
 

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