Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Multi Values from Module 1

Status
Not open for further replies.

Sorrells

Programmer
Dec 28, 2000
92
US
Hello,

I want to pass 2 variables from two different forms to a common routine. The routine needs to return 4 values to the calling statement.

I am unsure what structures can handle this. I would appreciate any advice.
Regards, Sorrells
 
You have a number of alternatives

All function parameters are passed 'by reference' (unless you explicitly state ByVal in the param list).

This means that if you update a function parameter value in a function, the supplied value is changed permanently.

Therefore, one option is to simply add four parameters to your function.

Another option is to create a user-defined type, e.g.
Type MyType
Val1 as integer
Val2 as string
...
end type

then, you can declare your function to return a value of type MyType

function MyFunction(Param1 as integer, param2 as double) as mytype
....
dim RetVal as MyType
RetVal.Val1 = 100
RetVal.Val2 = "whatever"
....

MyFunction = RetVal
end function

another option might be to encode all the return values into a string and then decode them back out (which is often used with the openargs property of a form)

The most stylish answer would be to use the user-defined type. However, the context in which you use the function may limit your options.
 
Beetree,

Thanks for or response. I went with the function and it worked just fine! I was running on a paradigm that a function could return only one value. The call and function statements below allow for changes in all 4 variables passed.

I called the function from 2 forms but now can keep the common code in a single location for future maintenance. Four variables are passed to the function as noted in the call below.

Call Get_Task_Sched(Schedule_Opt, Default_Freq, Default_Week, Default_Day)

The function is located in it’s own module at this time, Task_Freq_ID. Within the module, it is identified as follows:

Public Function Get_Task_Sched(Schedule_Opt As Integer, Default_Freq As Integer, _
Default_Week As Integer, Default_Day As Integer)

All the values made in the call to the function might be changed when the function has completed its work for the form's to take advantqage of.

I appreciate your help!
Regards, Sorrells
 
Any time! By the way, one other way to do it, and perhaps the best way, is as follows:

type TaskSchedType
Schedule_Opt as <whatever>
Default_Freq as <whatever>
Default_Week as <whatever>
Default_Day as <whatever>
end type

public function Get_Task_Sched(TaskSchedInfo as TaskSchedType) as boolean
Get_Task_Sched = false
TaskSchedInfo.Default_Freq = <whatever>
end function

to call it:
Dim Params as TaskSchedType
Params.Default_Freq = me![Frequency]
...
if not Get_Task_Sched(Params) then
msgbox &quot;whatever&quot;
endif


 
Another aproach is (assuming all of hte retun values are typed the same) to return an array. Even the limitation of same type can be overcome by typing the return as Varriant, however this DOES incur a performance penalty, and may require explicit coercion to the desired data type within the calling object.

On the other side of the discussion, it SHOOULD be notewd that there is some 'coriosity' in wanting / needing a procedure whic returns different results to specific calling objects. On the surface, the different calling objects would appear to need to call seperate routines.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Very good! Thanks to both of you.

I am happy with my function as is but have added both of your comments to my notes. Regards, Sorrells
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top