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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning Values from Standard Modules 1

Status
Not open for further replies.

tomhughes

Vendor
Aug 8, 2001
233
0
0
US
How do I return several Values of different types from a Standard Module in VB6? From the script below I am only returning a string,but I need to return several different String values, and ineteger values.


Code:
Public Function GetTopBM(ByRef strL, ByRef strR, ByRef y) As String
< User Code >
GetTopBM = 'SomeString"
End Function


Calling the Module 
strTopBM = Module1.GetTopBM(strL, strR, y)
 
Pretty much the same question you asked here. thread705-1334081


 
Brief example. Put all of this in a form with a command button named command1. You will see the results in the Immediate window(ctrl + G)
Code:
Option Explicit

Private Sub Command1_Click()
Dim returnInt       As Integer
Dim returnStr       As String
Dim returnbol       As Boolean

Debug.Print ReturnValues(returnInt, returnStr, returnbol)
Debug.Print returnInt
Debug.Print returnStr
Debug.Print returnbol


End Sub
Function ReturnValues(ByRef myint As Integer, ByRef mystr As String, ByRef mybol As Boolean) As String

myint = 8
mystr = "Returned from Function"
ReturnValues = "It works"
mybol = True

End Function
 
Thanks jadams0173 - Very well Done - I appreciate yoiur time
 
You can also treat the module as a poor man's Class with code like this;

Poor because it can only have one instance and will remain in memory for the duration of the app.

Put this on a Form containing just Command1;

Private Sub Form_Load()

Module1.Name = "module one"

End Sub

Private Sub Command1_Click()

Print Module1.Name
' "module one" should appear on the Form at top left

End Sub

Put this in a module named Module1;

Dim mMyProp

Public Property Let Name(Name2Set)

mMyProp = Name2Set

End Property

Public Property Get Name()

Name = mMyProp

End Property

If you want to do similar stuff with UDTs checkout the Friend Sub/ Function modifier in vb help.
 
The point is that when you pass variables by reference, you're actually "passing the variable itself", i. e. a reference to the variable, rather than the value that the variable is equal to.
 
Passing Variables has been confusing to me, because I thought that the Variables being passed were in a different location than the Variables being received from the Function. However, after playing around with it a little it appears that the Variables that you put in the Call statement, and their appropriate order in the Function, can actually be either Variables that you pass to the Function, or the Variables that you receive back from the Function. The other thing I played around with, was Declaring the Variables to be Public, and just writing to the Public Variable using the ByRef call, then retrieving the variable from another Function in another Module. However it does not appear that you can actually get the updated value, until that instance of the call is complted.
 
<can actually be either Variables that you pass to the Function, or the Variables that you receive back from the Function

The point about a "reference" is that it is pointing to some place in memory. When you pass a variable by reference (ByRef, which is the default), then, you are passing into the function a pointer to a variable. If the function modifies the data that the pointer points to (by modifying one of its, the function's, arguments), then (obviously) everything that points to that location will point to the modified data.

Passing by value (ByVal) creates a new memory location, stores the data that the variable passes to that new memory location, and goes from there. The link to the passed variable is broken.

So, when you pass by reference, think of it as two different variables referencing the same data. That's why it works the way it does.

<Declaring the Variables to be Public, and just writing to the Public Variable using the ByRef call

If you do this, it's irrelevant whether you pass ByVal or ByRef. You're modifying a different variable in either case.

<However it does not appear that you can actually get the updated value, until that instance of the call is complted.

I'm not sure what you mean by "instance of the call". If you mean what I think you mean, yes, you can get the updated value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top